/*
Opioid Use, Health and Crime replication file 

This .do file builds all analyses data and replicates all results in the paper 
as well as the online appendix. 

Please refer to the README.txt for a description of files 

Code to create all figures and tables begins at line 1611 in this do file

*/

cd "INSERT PATH TO FOLDER WHERE replication.do is saved"
/*
create a /data subfolder and place all data files there
create a /results subfolder for all table and figure output
*/

/*******************************************************************************
********************************************************************************
********************************************************************************
CLEAN DATA 
********************************************************************************
********************************************************************************
********************************************************************************/

*
*criminal charge data from NSW Bureau of Crime Statistics and Research (BOCSAR) [RESTRICTED]
*
insheet using "data/rod15097_finalised_charge.csv", clear
*to count total offenses
gen off_all=1
/*
classify crime types based on ANZSOC 2011 codes: http://www.ausstats.abs.gov.au/Ausstats/subscriber.nsf/0/5CE97E870F7A29EDCA2578A200143125/$File/12340_2011.pdf
*/
gen off_violent=inrange(anzsoc,100,699)
gen off_property=inrange(anzsoc,700,999)
gen off_drug=inrange(anzsoc,1000,1099)
gen off_other=inrange(anzsoc,1100,9999)
gen off_code_missing=anzsoc==.
gen off_murder=inrange(anzsoc,100,111) 
*murder plus includes attempted and manslaughter charges and driving causing death
gen off_murder_plus=inrange(anzsoc,100,199)
gen off_assault=inrange(anzsoc,200,299)
	gen off_assault_seriousinj=anzsoc==211
	gen off_assault_seriousnoninj=anzsoc==212
	gen off_assault_common=anzsoc==213
gen off_sexassault=inrange(anzsoc,300,399)
*include child negligence and breach of domestic order as "off_domestic"
gen off_domestic=(inrange(anzsoc,491,499) | inrange(anzsoc,1531,1532))
gen off_abduction=inrange(anzsoc,511,521)
gen off_harrassment=inrange(anzsoc,531,532)
gen off_robbery=inrange(anzsoc,610,619)
gen off_breakenter=inrange(anzsoc,710,719)
gen off_theft=inrange(anzsoc,800,899)
gen off_fraud=inrange(anzsoc,900,999)
gen off_propdamage=inrange(anzsoc,1210,1219)
gen off_puborder=inrange(anzsoc,1300,1399)
gen off_traffic=inrange(anzsoc,1400,1499)
gen off_mv_theft=inrange(anzsoc,811,812)
*drug types - isolate primary categories - heroin will fall under opiates
gen drugtype="amph" if drug=="Amphetamines"
replace drugtype="cannabis" if drug=="Cannabis"
replace drugtype="cocaine" if drug=="Cocaine"
replace drugtype="opiates" if drug=="Opiates"
replace drugtype="other" if inlist(drug,"Barbiturates","Ecstasy", "Hallucinogens","Other drug")
replace drugtype="missing" if off_drug==1 & drug=="" 
foreach t in "amph" "cannabis" "cocaine" "opiates" "other" "missing"  {
gen off_drug_`t'=drugtype=="`t'"
gen off_drug_`t'_use=(drugtype=="`t'" & inlist(anzsoc,1041,1042,1022))
gen off_drug_`t'_sale=(drugtype=="`t'" & inlist(anzsoc,1031,1032,1021,1011,1012))

}
gen off_driving_alcohol=inlist(anzsoc,132,411,412,1431)
gen off_driving_influence = inrange(anzsoc,411,411)
gen off_driving_death =inlist(anzsoc,132,132)
gen off_prostitution=inlist(anzsoc,1324)
*code dates using offense date listed
split offence_date, p("/")
drop offence_date
destring offence_date*, replace
gen year=offence_date3
gen off_date_mdy=mdy(offence_date2,offence_date1,offence_date3)
gen ym=mofd(off_date_mdy)
*keep observations from 1994 through 2015
keep if inrange(ym,ym(1994,1),ym(2015,12))
drop offence_date* off_date_mdy

collapse (sum) off_* , by(ym ppn)
save "data/offense_ym.dta", replace

*
* opioid treatment participation data from NSW Department of Health's Pharmaceutical Drugs of Addiction System (PHDAS) [RESTRICTED]
*
insheet using "data/schnepel_OTP_file.csv", clear

duplicates drop //only 24 obs duplicate across all obs
gen start=progstart
gen end=progend
foreach v in "start" "end" {
split prog`v', p("/")
drop prog`v'
destring prog`v'*, replace
gen prog`v'_y=prog`v'3+1900 if inrange(prog`v'3,20,99)
replace prog`v'_y=prog`v'3+2000 if inrange(prog`v'3,00,19)
gen prog`v'_mdy=mdy(prog`v'2,prog`v'1,prog`v'_y)
drop prog`v'1 prog`v'2 prog`v'3 prog`v'_y
}
*Exclude if 
*1) on temporary program (usually interstate transfers)
drop if typeprog=="temporary" //8033 episodes
*2) registered on buprenorphine clinical trial
drop if typeprog=="trial" //128 episodes
*4) had same start and end date, 
drop if progstart_mdy==progend_mdy // 1584 episodes
*5) end date preceded the start date
drop if progstart_mdy>progend_mdy //176 episodes
*drop if end date is after 2016 (and is not missing)
drop if progend_mdy>mdy(12,31,2016) & progend_mdy~=. // 3 obs
*some obs have no program end date and others end date overlaps with next start date
*  assume still on at 12/2016 if no end date and no subsequent start date
* first, replace end date is start date if subsequent program and create flag
sort ppn progstart_mdy
bys ppn: gen progend_imputed="used start date of next" if ((progstart_mdy[_n+1]~=. & progend_mdy==.) | (progstart_mdy[_n+1]<progend_mdy & progend_mdy~=.))
bys ppn: replace progend_mdy=progstart_mdy[_n+1] if progend_imputed=="used start date of next" //934 real changes made
replace progend_imputed="used 12/31/2016 as end date" if progend_mdy==.
replace progend_mdy=mdy(12,31,2016) if progend_mdy==. //13,558 real changes made
/*
Episode of continuous treatment – no more than a 6-day break between treatment programmes / if gap of 7 days or greater, define as a new episode of treatment [Definition adopted by  Burns et al. 2009 after consultation with experts in clinical research and practice]
*/
sort ppn progstart_mdy progend_mdy
bys ppn: gen episode_n_ppn=_n 
bys ppn: replace episode_n_ppn=episode_n_ppn[_n-1] if progstart_mdy<progend_mdy[_n-1]+6 & episode_n_ppn>1
drop start end
gen startdose_bupr=startdose if drug=="buprenorphine"
replace startdose=. if drug=="buprenorphine"
gen maxdose_bupr=maxdose if drug=="buprenorphine"
replace maxdose=. if drug=="buprenorphine"
gen progdays=progend_mdy-progstart_mdy
gen progdays_bupr=progdays if drug=="buprenorphine"
replace progdays=. if drug=="buprenorphine"
*now just one obs per episode of continuous treatment
collapse (min) startdose* progstart_mdy (max) maxdose* progend_mdy   (sum) progdays* (last) typeprog leaving progend_imputed, by(ppn episode_n_ppn)
drop episode_n_ppn  
gen ym_start=ym(year(progstart_mdy),month(progstart_mdy))
gen ym_end=ym(year(progend_mdy),month(progend_mdy))
drop if ym_start>ym(2015,12) //1562 episodes deleted

sort ppn ym_start ym_end
bys ppn: gen ppn_prog_n=_n
egen ppn_prog_id=group(ppn ppn_prog_n)

global progdetails "typeprog startdose startdose_bupr maxdose maxdose_bupr progdays progdays_bupr leaving progstart_mdy progend_mdy progend_imputed"
preserve
sort ppn ppn_prog_n
keep ppn ppn_prog_id $progdetails
tempfile progdetails
save `progdetails'
restore

keep ppn_prog_id ym_start ym_end

reshape long ym_, i(ppn_prog_id) j(period) string
drop period
rename ym_ ym
sort ppn_prog_id ym
bys ppn_prog_id: drop if _n==_N & ym==ym[_n-1] //drop one obs if start and end same month
xtset ppn_prog_id ym
tsfill //fill in so have an obs for each month in phdas program
keep if inrange(ym,ym(1985,1),ym(2015,12))

merge m:1 ppn_prog_id using `progdetails'
drop _merge

/*
•	early drop-out <3 days for withdrawal management, <7 days for 
residential rehab, <28 days for counselling, <one month for pharmacotherapy 
[ defined by Degenhardt et al. 2005 in consultation with researchers at 
the National Drug and Alcohol Research Centre]
*/
gen progdays_total=progdays+progdays_bupr
gen early_drop_out=0
replace early_drop_out=1 if (typeprog=="withdrawal" & progdays_total<3)
replace early_drop_out=1 if (typeprog=="maintenance" & progdays_total<28)

gen phdas_beg_ym=(progstart_mdy<dofm(ym))
gen phdas_beg_type=typeprog if phdas_beg_ym==1
gen phdas_beg_ym_days_on=0
replace phdas_beg_ym_days_on=dofm(ym)-progstart_mdy if phdas_beg_ym==1
gen phdas_beg_ym_startdose=startdose if phdas_beg_ym==1
gen phdas_beg_ym_startdose_bupr=startdose_bupr if phdas_beg_ym==1
gen phdas_beg_ym_maxdose=maxdose if phdas_beg_ym==1
gen phdas_beg_ym_maxdose_bupr=maxdose_bupr if phdas_beg_ym==1

gen phdas_new_ym=inrange(progstart_mdy,dofm(ym),dofm(ym+1)-1)
gen phdas_new_type=typeprog if phdas_new_ym==1
gen phdas_new_ym_startdose=startdose if phdas_new_ym==1
gen phdas_new_ym_startdose_bupr=startdose_bupr if phdas_new_ym==1
gen phdas_new_ym_maxdose=maxdose if phdas_new_ym==1
gen phdas_new_ym_maxdose_bupr=maxdose_bupr if phdas_new_ym==1

gen phdas_end_ym=inrange(progend_mdy,dofm(ym),dofm(ym+1)-1)
gen phdas_end_ym_earlydropout=0
replace phdas_end_ym_earlydropout=1 if early_drop_out==1 & phdas_end_ym==1
gen phdas_end_ym_days_on=progend_mdy-progstart_mdy if phdas_end_ym==1
gen phdas_end_leaving=leaving if phdas_end_ym==1

gen phdas_days_ym=dofm(ym+1)-dofm(ym) if (progstart_mdy<dofm(ym) & progend_mdy>=dofm(ym+1)) //full month - 95% of episodes
replace phdas_days_ym=progend_mdy-progstart_mdy if ((inrange(progstart_mdy,dofm(ym),dofm(ym+1)-1) & inrange(progend_mdy,dofm(ym)+1,dofm(ym+1))) & phdas_days_ym==.) //end and begin in month
replace phdas_days_ym=dofm(ym+1)-progstart_mdy if (progstart_mdy>=dofm(ym) & phdas_days_ym==.)
replace phdas_days_ym=progend_mdy-(dofm(ym)-1) if (progend_mdy<dofm(ym+1) & phdas_days_ym==.)

sort ppn progstart_mdy progend_mdy

collapse (count) ppn_prog_id (first) phdas_beg_type (last) phdas_new_type phdas_end_leaving (max) phdas_beg_ym* phdas_new_ym* phdas_end_ym* (sum) phdas_days_ym, by(ppn ym)

rename ppn_prog_id n_phdas_episodes_ym

save "data/phdas_ym.dta", replace

*
* further cleaning of opioid treatment participation data from NSW Department of Health's Pharmaceutical Drugs of Addiction System (PHDAS) [RESTRICTED]
*
insheet using "data/schnepel_OTP_file.csv", clear

duplicates drop //24 obs duplicate across all obs

gen start=progstart
gen end=progend

foreach v in "start" "end" {
split prog`v', p("/")
drop prog`v'
destring prog`v'*, replace
gen prog`v'_y=prog`v'3+1900 if inrange(prog`v'3,20,99)
replace prog`v'_y=prog`v'3+2000 if inrange(prog`v'3,00,19)
gen prog`v'_mdy=mdy(prog`v'2,prog`v'1,prog`v'_y)
drop prog`v'1 prog`v'2 prog`v'3 /*prog`v'_y*/
}

*** sample cleaning 
*registered on buprenorphine clinical trial, 
drop if typeprog=="trial" //128 episodes
*start date and end date are the same 
drop if progstart_mdy==progend_mdy // 1584 episodes
*start date after end date 
drop if progstart_mdy>progend_mdy //176 episodes
*drop if exit was from a "data clean up" 
drop if leaving==90 // 520 obs
*get rid of early cases
drop if progend_y<1994
*drop if end date is after 2016 (and is not missing)
drop if progend_mdy>mdy(12,31,2016) & progend_mdy~=. // 3 obs
replace progend_mdy=mdy(12,31,2016) if progend_mdy==.

sort ppn progstart_mdy progend_mdy
bys ppn: gen episode_n=_n

egen id=group(ppn episode_n)
bys ppn: egen episode_max=max(episode_n)

*** calculating gaps between treatment episodes
bys ppn: gen gap1=progstart_mdy - progend_mdy[_n-1] if episode_n~=1
bys ppn: gen gap2=progstart_mdy[_n+1] - progend_mdy if episode_n~=episode_max

* restriction - dropping episodes 7 days or shorter
local gap 7
replace progstart_mdy=. if gap1<=`gap'
replace progend_mdy=. if gap2<=`gap'

* restriction - dropping transfers across programs
replace progend_mdy=. if leaving>=25
replace progstart_mdy=. if (leaving[_n-1]>=25 & episode_n~=1)

* cleaning up the episodes
replace progstart_mdy=progstart_mdy[_n-1] if progstart_mdy==.
drop if progend_mdy==.
bys ppn: gen episode_n2=_n
bys ppn: egen episode_max2=max(episode_n2)
keep ppn leaving progstart_mdy progend_mdy gap2

sort ppn progstart_mdy progend_mdy
bys ppn: gen episode_n=_n
egen id=group(ppn episode_n)
bys ppn: egen episode_max=max(episode_n)

*** count entries and exits (by type of exit) by yq and ppn
preserve
gen yqBEG=yq(year(progstart_mdy), quarter(progstart_mdy))
gen yqEND=yq(year(progend_mdy), quarter(progend_mdy))
keep yq* ppn leaving id episode_n episode_max
drop if yqBEG==.
drop if yqEND==.
reshape long yq, i(ppn leaving id episode_n) j(period) string
gen otp_entries=1 if period=="BEG"
gen otp_exits=1 if period=="END" 
gen otp_exits_success=1 if period=="END" & leaving==22
gen otp_exits_failure=1 if period=="END" & leaving~=22
gen tx_out=1 if otp_exits==1
replace tx_out=0 if otp_entries==1

collapse (first) ppn tx_out first=episode_n (last) last=episode_n (sum) otp_* , by(id yq)

xtset id yq
tsfill
foreach var of varlist otp_* {
replace `var'=0 if `var'==.
}
bys id: carryforward ppn tx_out first last, replace
replace tx_out=1 if otp_entries>=1
rename tx_out otp_out

collapse (first) first (last) last (sum) otp_* , by(ppn yq)
replace otp_out=1 if otp_out>=1
gen otp_reentries=otp_entries 
replace otp_reentries=otp_entries - 1 if last==1 & otp_entries>=1
replace otp_out=0 if otp_reentries==0
tempfile otp1
save `otp1', replace
restore

*** count days on otp
keep ppn id progstart_mdy progend_mdy
rename progstart_mdy dateBEG
rename progend_mdy dateEND
reshape long date, i(ppn id) j(type) string

xtset id date
tsfill 
bys id: carryforward ppn, replace
gen yq=yq(year(date),quarter(date))
gen otp_days_yq=1

***work out the maximum days in the quarter
preserve
sort yq
collapse (max) otp_days_yq_max=otp_days_yq, by(yq)
tempfile otpmax
save "data/otp_days_max.dta", replace
restore
***

collapse (sum) otp_days_yq, by(ppn yq)
sort ppn yq
merge 1:1 ppn yq using `otp1'
drop _merge

save "data/otp_yq.dta", replace


*
*demographic data pulled from NSW Bureau of Crime Statistics and Research (BOCSAR) [RESTRICTED]
*
insheet using "data/rod15097_finalised_charge.csv", clear
keep ppn gender dob indig finalisation_date lga
/*
some ppns are associated with different dobs or indig indicators - keep mode and flag when multiple entries
*/
split finalisation_date, p("/")
drop finalisation_date
destring finalisation_date*, replace
gen d_lastym_bocsar=ym(finalisation_date3,finalisation_date2)

bys ppn: egen d_indig_bocsar=mode(indig), maxmode
replace d_indig_bocsar=. if d_indig_bocsar==2 // approx 4 percent of obs have indig unknown or missing

*create sex=1 if male and 2 if female to be consistent with other datasets - will create a female indicator later
gen sex=1 if gender==1
replace sex=2 if gender==0
bys ppn: egen d_sex_bocsar=mode(sex), maxmode

gen d_lga=lga if ym(finalisation_date3,finalisation_date2)<=ym(2000,12)
bys ppn: egen d_lga_bocsar=mode(lga), maxmode

split dob, p("/")
destring dob3, gen(birth_year)
gen d_birthyear_bocsar=birth_year
destring dob2, gen(birth_month)
gen d_birthmonth_bocsar=birth_month

sort ppn d_lastym_bocsar
bys ppn: keep if _n==_N // keep obs from last appearance in bocsar data

keep ppn d_birthyear_bocsar d_birthmonth_bocsar d_sex_bocsar d_lastym_bocsar d_indig_bocsar d_lga_bocsar

tempfile bocsar
save `bocsar', replace

*
* demographic data from EDDC - Emergency Department Data Collection records information on public hospital ER visits [RESTRICTED]
*
insheet using "data/eddc_2016_03_1.csv", clear

gen ppn=projectpersonnumber
*calc mode birthyear and gender for those with multiple obs, maxmode option just chooses highest if multiple modes
bys ppn: egen d_birthyear_eddc=mode(birth_year), maxmode 
replace d_birthyear_eddc=. if (d_birthyear_eddc==9999) // 13 have 9999 birth year

bys ppn: egen d_sex_eddc=mode(sex), maxmode 
replace d_sex_eddc=. if d_sex_eddc>=3 //5 cases of sex=3 and 8 cases of sex=9

gen d_lastym_eddc=ym(arrival_year, arrival_month)
keep if inrange(d_lastym_eddc,ym(1994,1),ym(2016,12)) //0 obs deleted

sort ppn d_lastym_eddc
bys ppn: keep if _n==_N // keep obs from last appearance in eddc data

keep ppn d_birthyear_eddc d_sex_eddc d_lastym_eddc
tempfile eddc
save `eddc', replace

*
* demographic data from APDC - Admitted Patient Data Collection records information on inpatient admissions to public hospitals [RESTRICTED]
*

insheet using "data/apdc_2016_03_1.csv", clear

gen ppn=projectpersonnumber
bys ppn: egen d_birthyear_apdc=mode(birth_year), maxmode 
replace d_birthyear_apdc=. if (d_birthyear_apdc==9999 | birth_year<1900) // one 9999 birth years, 1 as 1800

bys ppn: egen d_sex_apdc=mode(sex), maxmode 
replace d_sex_apdc=. if d_sex_apdc==9 //14 cases of sex==9

gen d_lastym_apdc=ym(episode_start_year, episode_start_month)
keep if inrange(d_lastym_apdc,ym(1994,1),ym(2016,12)) //0 obs deleted

sort ppn d_lastym_apdc
bys ppn: keep if _n==_N // keep obs from last appearance in apdc data

keep ppn d_birthyear_apdc d_sex_apdc d_lastym_apdc
tempfile apdc
save `apdc', replace 

*
* demographic data from RBDM - NSW Registry of Births Deaths and Marriages [RESTRICTED]
*

insheet using "data/rbdmdths_2016_03_1.csv", clear
gen ppn=projectpersonnumber

*a few hundred ppns with more than one obs in death file - keep obs with non-missing birth_year
collapse (min) birth_year death_year death_month, by(ppn)

rename birth_year d_birthyear_rbdm
gen d_lastym_rbdm=ym(death_year, death_month)
keep if inrange(d_lastym_rbdm,ym(1994,1),ym(2016,12)) //0 obs deleted

keep ppn d_birthyear_rbdm d_lastym_rbdm
tempfile rbdm
save `rbdm'

***merge demographic data

use `bocsar', clear
gen d_birthyear=d_birthyear_bocsar
gen d_birthyear_source="BOCSAR"
gen d_birthmonth=d_birthmonth_bocsar
gen d_birthmonth_source="BOCSAR"
gen d_sex=d_sex_bocsar
gen d_sex_source="BOCSAR"
gen d_indig=d_indig_bocsar
gen d_indig_source="BOCSAR"

merge 1:1 ppn using `eddc'
replace d_birthyear=d_birthyear_eddc if _merge==2
replace d_birthyear_source="EDDC" if _merge==2
replace d_sex=d_sex_eddc if _merge==2
replace d_sex_source="EDDC" if _merge==2
drop _merge

merge 1:1 ppn using `apdc'
replace d_birthyear=d_birthyear_apdc if _merge==2
replace d_birthyear_source="APDC" if _merge==2
replace d_sex=d_sex_apdc if _merge==2
replace d_sex_source="APDC" if  _merge==2
drop _merge

merge 1:1 ppn using `rbdm'
replace d_birthyear=d_birthyear_rbdm if _merge==2
replace d_birthyear_source="RBDM" if _merge==2
drop _merge

order ppn d_birthyear d_birthmonth d_sex d_indig d_lga_bocsar d_birthyear_source d_birthmonth_source d_sex_source d_indig_source 
keep ppn d_birthyear d_birthmonth d_sex d_indig_bocsar d_lga_bocsar d_birthyear_source d_birthmonth_source d_sex_source d_indig_source

save "data/demographic.dta", replace 

*
*death registration data from the NSW Registry of Births Deaths and Marriages (RBDM), which includes information on individuals' date of death, age at death, sex and Indigenous status. [RESTRICTED]
*we also use the Cause of Death Unit Record File,which includes causes of death classified according to International Classification of Disease (ICD) systems [RESTRICTED]
*

insheet using "data/rbdmdths_2016_03_1.csv", clear

gen mortality=1
gen ym=ym(death_year,death_month)

keep if inrange(ym,ym(1994,1),ym(2015,12))

rename sa2_2011_code mortality_sa2_2011
rename projectpersonnumber ppn

keep ppn ym mortality mortality_sa2_2011
duplicates drop //131 observations deleted

bys ppn ym: keep if _n==1 //12 individuals have multiple death records - ym of death same in each, some missing sa2 code and others not
bys ppn: keep if _n==1 // 1 obs with two different months of death - keep first

order ppn ym mortality mortality_sa2_2011
tempfile mortality
save `mortality'

*use cause of death data and ICD codesto identify type of death
insheet using "data/codurf_2016_03_ucod_nodecimal.csv", clear

*breaking up the underlying cause of death coding
gen ICD10_1=substr(ucod_recode,1,1) if ucod_codeset=="ICD10"
gen ICD10_2=substr(ucod_recode,2,2) if ucod_codeset=="ICD10"
gen ICD10_3=substr(ucod_recode,1,3) if ucod_codeset=="ICD10"
destring ICD10_2, replace

*breaking up all causes of death coding
forvalues i=1/20 {
gen racs`i'_11=substr(racs`i',1,1) if ucod_codeset=="ICD10"
gen racs`i'_22=substr(racs`i',2,2) if ucod_codeset=="ICD10"
gen racs`i'_23=substr(racs`i',2,3) if ucod_codeset=="ICD10"
destring racs`i'_22, replace
destring racs`i'_23, replace
}

***************************** drugs and alcohol ****************************

*** overdoses

* overdoses - narrow
gen overdose1=0
replace overdose1=1 if ICD10_1=="X" & inrange(ICD10_2,40,44)
replace overdose1=1 if ICD10_1=="X" & inrange(ICD10_2,60,64)
replace overdose1=1 if ICD10_1=="X" & ICD10_2==85
replace overdose1=1 if ICD10_1=="Y" & inrange(ICD10_2,10,14)
*Y352 DOES NOT EXIST

tab overdose1

* overdoses - broad
gen overdose2=0
replace overdose2=1 if overdose1==1
forvalues i=1/20 {
replace overdose2=1 if racs`i'_11=="X" & inrange(racs`i'_22,40,44)
replace overdose2=1 if racs`i'_11=="X" & inrange(racs`i'_22,60,64)
replace overdose2=1 if racs`i'_11=="X" & racs`i'_22==85
replace overdose2=1 if racs`i'_11=="Y" & inrange(racs`i'_22,10,14)
}
tab overdose2

*** opiates/heroin at various levels of breadth
gen heroin=0
gen opiates1=0
gen opiates2=0
gen opiates3=0

forvalues i=1/20 {
replace heroin=1 if racs`i'_11=="T" & racs`i'_23==401
replace opiates1=1 if racs`i'_11=="T" & inrange(racs`i'_23,400,404)
replace opiates1=1 if racs`i'_11=="T" & racs`i'_23==406
replace opiates2=1 if opiates1==1
replace opiates2=1 if racs`i'_11=="F" & racs`i'_22==11
replace opiates3=1 if opiates2==1
replace opiates3=1 if racs`i'_11=="F" & racs`i'_22==19
}

tab heroin /* heroin is clearly too narrow - only here for information*/
tab opiates1
tab opiates2
tab opiates3

*** all drugs
gen drugs=0

forvalues i=1/20 {
replace drugs=1 if racs`i'_11=="F" & inrange(racs`i'_22,11,19)
replace drugs=1 if racs`i'_11=="X" & inrange(racs`i'_22,40,44)
replace drugs=1 if racs`i'_11=="X" & racs`i'_22==46

replace drugs=1 if racs`i'=="O355" 
replace drugs=1 if racs`i'=="P044" 
replace drugs=1 if racs`i'=="P961"
replace drugs=1 if racs`i'=="T387"
replace drugs=1 if racs`i'_11=="T" & racs`i'_22==40
replace drugs=1 if racs`i'=="T436"

* adding in X60-64 and X85 for overdoses
replace drugs=1 if racs`i'_11=="X" & inrange(racs`i'_22,60,64)
replace drugs=1 if racs`i'_11=="X" & racs`i'_22==85

replace drugs=1 if racs`i'_11=="Y" & inrange(racs`i'_22,10,14)
replace drugs=1 if racs`i'_11=="Y" & racs`i'_22==16

* additional to Evans and Moore, but used for drugs (adds 6 cases)
replace drugs=1 if racs`i'=="T509"
}

tab drugs
tab drugs opiates3
tab drugs overdose2


*** all drugs and alcohol

*** all drugs
gen drugsalc=0
replace drugsalc=1 if drugs==1

forvalues i=1/20 {
replace drugsalc=1 if racs`i'_11=="F" & racs`i'_22==10

replace drugsalc=1 if racs`i'=="G312"
replace drugsalc=1 if racs`i'=="G621" 
replace drugsalc=1 if racs`i'=="G721"
replace drugsalc=1 if racs`i'=="I426"
replace drugsalc=1 if racs`i'=="K292"
replace drugsalc=1 if racs`i'_11=="K" & racs`i'_22==70
replace drugsalc=1 if racs`i'_11=="K" & racs`i'_22==73
replace drugsalc=1 if racs`i'_11=="K" & racs`i'_22==74
replace drugsalc=1 if racs`i'=="K860"
replace drugsalc=1 if racs`i'=="O354"
replace drugsalc=1 if racs`i'=="P043"
replace drugsalc=1 if racs`i'=="Q860"
replace drugsalc=1 if racs`i'=="T510"
replace drugsalc=1 if racs`i'=="T511"
replace drugsalc=1 if racs`i'=="T519"
replace drugsalc=1 if racs`i'=="X45"
replace drugsalc=1 if racs`i'=="Y15"

}

tab drugsalc
tab drugsalc drugs

***************************** cause of death ****************************

tab ICD10_1 

gen cancer=0
replace cancer=1 if ICD10_1=="C"
gen heart=0
replace heart=1 if ICD10_1=="I"
gen external=0
replace external=1 if inlist(ICD10_1,"V","W","X","Y")

rename projectpersonnumber ppn


/*
categories from Degenhardt_2014

mortality_accidental_drug: F11-F16, F19 / F55 / X40-X44
mortality_suicide: X60-X84 / Y870
mortality_accidental_injury: V01-V04, V06, V09-V80, 
V810-V811, V820-V821, V83-V87, V880-V885, V887-V888, V89, V99 / Y85
mortality_violence: X85-Y09 / Y871
*/

* overdoses - narrow
gen accidental_drug=0
replace accidental_drug=1 if ICD10_1=="F" & inrange(ICD10_2,11,16)
replace accidental_drug=1 if ICD10_1=="F" & ICD10_2==19
replace accidental_drug=1 if ICD10_1=="F" & ICD10_2==55
replace accidental_drug=1 if ICD10_1=="X" & inrange(ICD10_2,40,44)

gen suicide=0
replace suicide=1 if ICD10_1=="X" & inrange(ICD10_2,60,84)
replace suicide=1 if ucod_recode=="Y870" 


gen accidental_injury=0
replace accidental_injury=1 if ICD10_1=="V" & inrange(ICD10_2,1,4)
replace accidental_injury=1 if ICD10_1=="V" & ICD10_2==6
replace accidental_injury=1 if ICD10_1=="V" & inrange(ICD10_2,9,80)
replace accidental_injury=1 if ICD10_1=="V" & ICD10_2==81
replace accidental_injury=1 if ICD10_1=="V" & ICD10_2==82
replace accidental_injury=1 if ICD10_1=="V" & inrange(ICD10_2,83,87)
replace accidental_injury=1 if ICD10_1=="V" & ICD10_2==88
replace accidental_injury=1 if ICD10_1=="V" & ICD10_2==89
replace accidental_injury=1 if ICD10_1=="V" & ICD10_2==99
replace accidental_injury=1 if ICD10_1=="Y" & ICD10_2==85

gen violence=0
replace violence=1 if ICD10_1=="X" & inrange(ICD10_2,85,99)
replace violence=1 if ICD10_1=="Y" & inrange(ICD10_2,0,9)
replace violence=1 if ucod_recode=="Y871" 


keep ppn ucod_recode ucod_codeset overdose1-external accidental_drug suicide accidental_injury violence

foreach var of varlist ucod_recode ucod_codeset overdose1-external accidental_drug suicide accidental_injury violence {
rename `var' mortality_`var'
}

duplicates drop 
*still 13 with conflicting codes, drop the inconclusive codes
duplicates tag ppn, gen(dup)
drop if dup==1 & mortality_ucod_recode=="R99"
*still 11 with duplicates, keep second obs
duplicates tag ppn, gen(dup2)
bys ppn: drop if dup2==1 & _n==1
drop dup dup2

merge 1:1 ppn using `mortality'
drop _merge

save "data/mortality_ym.dta", replace


* 
* further clean up of icd codes using Cause of Death Unit Record File [RESTRICTED]
*

import delimited "data/codurf_2016_03_ucod_nodecimal.csv", clear 

gen ym=ym(death_year,death_month)

keep projectpersonnumber ym ucod_recode ucod_codeset death_year racs1-racs20 record_axis_count revision_status

rename projectpersonnumber ppn
sort ppn
gen mortality=1

* add single missing value
replace ucod_codeset="ICD10" if ppn==109828

* identify duplicates and separate where ucod_recode is different
duplicates tag ppn, gen(tag)
duplicates tag ppn ucod_recode, gen(tag2)

* drop death duplicates - I went through each pair and selected codes
drop if tag==1 & tag2==0 & (ucod_recode=="I10"|ucod_recode=="I330"|	///
ucod_recode=="I456"|ucod_recode=="J449"|ucod_recode=="K316"|ucod_recode=="Q208"|	///
ucod_recode=="R99"|ucod_recode=="X449"|ucod_recode=="X709"|ucod_recode=="Y209")		

duplicates drop ppn, force

tempfile ucod
save `ucod', replace

********************************************************************************
*** ICD9
use `ucod', clear

keep if ucod_codeset=="ICD09"
tab ucod_recode

*breaking up the underlying cause of death coding
gen ICD9_1=substr(ucod_recode,1,1) 
gen ICD9_3=substr(ucod_recode,1,3)
gen ICD9_24=substr(ucod_recode,2,3)
destring ICD9_24, replace

*************************************************************************
*** opiates - narrow

cap drop mortality_opiates
gen mortality_opiates=0

replace mortality_opiates=1 if ICD9_3=="291"
replace mortality_opiates=1 if ICD9_3=="304"
replace mortality_opiates=1 if ucod_recode=="3055"
replace mortality_opiates=1 if ucod_recode=="E8500"
replace mortality_opiates=1 if ucod_recode=="E8502"
replace mortality_opiates=1 if ucod_recode=="E8532"
replace mortality_opiates=1 if ucod_recode=="E8540"
replace mortality_opiates=1 if ucod_recode=="E8580"
replace mortality_opiates=1 if ucod_recode=="E8581"
replace mortality_opiates=1 if ucod_recode=="E8589"
replace mortality_opiates=1 if ucod_recode=="E9800"

replace mortality_opiates=1 if ucod_recode=="E9500"
replace mortality_opiates=1 if ucod_recode=="E9503"

tab mortality_opiates

********************************************************************************
*** all drugs - narrow

cap drop drugs
gen drugs=0
replace drugs=1 if  mortality_opiates==1

replace drugs=1 if ucod_recode=="E8505"
replace drugs=1 if ucod_recode=="E9501"
replace drugs=1 if ucod_recode=="E9504"
replace drugs=1 if ucod_recode=="E9505"
replace drugs=1 if ucod_recode=="E9506"

* adding hepatitis codes
replace drugs=1 if ICD9_3=="70"
replace drugs=1 if ucod_recode=="5714"
replace drugs=1 if ucod_recode=="5716"

tab mortality_opiates drugs

********************************************************************************
*** all drugs and alcohol - narrow

cap drop drugsalc
gen drugsalc=0
replace drugsalc=1 if drugs==1

replace drugsalc=1 if ICD9_3=="292"
replace drugsalc=1 if ICD9_3=="303"
replace drugsalc=1 if ucod_recode=="3575"
replace drugsalc=1 if ucod_recode=="4255"
replace drugsalc=1 if ucod_recode=="5353"
replace drugsalc=1 if ucod_recode=="5710"
replace drugsalc=1 if ucod_recode=="5711"
replace drugsalc=1 if ucod_recode=="5712"
replace drugsalc=1 if ucod_recode=="5713"
replace drugsalc=1 if ucod_recode=="7903"
replace drugsalc=1 if ucod_recode=="9473"
replace drugsalc=1 if ucod_recode=="9773"
replace drugsalc=1 if ICD9_3=="980"
replace drugsalc=1 if ucod_recode=="E860"

tab mortality_opiates drugsalc

gen mortality_opiates2=mortality_opiates
gen drugs2=drugs 
gen drugsalc2=drugsalc

desc

keep ppn ym mortality mortality_opiates mortality_opiates2 drugs drugs2 	///
	drugsalc drugsalc2 ucod_recode record_axis_count 

tempfile icd9
save `icd9', replace

********************************************************************************
********************************************************************************
*** ICD10

use `ucod', clear

keep if ucod_codeset=="ICD10"
tab ucod_recode

*breaking up the underlying cause of death coding
gen ICD10_1=substr(ucod_recode,1,1) 
gen ICD10_2=substr(ucod_recode,2,2) 
gen ICD10_3=substr(ucod_recode,1,3)
gen ICD10_23=substr(ucod_recode,2,3)
destring ICD10_2, replace
destring ICD10_23, replace

*breaking up all causes of death coding
forvalues i=1/12 {
gen racs`i'_11=substr(racs`i',1,1)
gen racs`i'_22=substr(racs`i',2,2)
gen racs`i'_23=substr(racs`i',2,3)
destring racs`i'_22, replace
destring racs`i'_23, replace
}

*************************************************************************
*** opiates - narrow

cap drop mortality_opiates
gen mortality_opiates=0

replace mortality_opiates=1 if ICD10_1=="X" & inrange(ICD10_2,40,44)
replace mortality_opiates=1 if ICD10_1=="X" & inrange(ICD10_2,60,64)
replace mortality_opiates=1 if ICD10_1=="X" & ICD10_2==85
replace mortality_opiates=1 if ICD10_1=="Y" & inrange(ICD10_2,10,14)
replace mortality_opiates=1 if ICD10_1=="Y" & ICD10_2==35

*Y352 DOES NOT EXIST

replace mortality_opiates=1 if ICD10_1=="T" & ICD10_23==401
replace mortality_opiates=1 if ICD10_1=="T" & inrange(ICD10_23,400,404)
replace mortality_opiates=1 if ICD10_1=="T" & ICD10_23==406
replace mortality_opiates=1 if ICD10_1=="F" & ICD10_2==11
replace mortality_opiates=1 if ICD10_1=="F" & ICD10_2==19

replace mortality_opiates=1 if ICD10_1=="R" & ICD10_2==99

*** opiates - broad
cap drop mortality_opiates2
gen mortality_opiates2=mortality_opiates

forvalues i=1/12 {
replace mortality_opiates2=1 if racs`i'_11=="X" & inrange(racs`i'_22,40,44)
replace mortality_opiates2=1 if racs`i'_11=="X" & inrange(racs`i'_22,60,64)
replace mortality_opiates2=1 if racs`i'_11=="X" & racs`i'_22==85
replace mortality_opiates2=1 if racs`i'_11=="Y" & inrange(racs`i'_22,10,14)
replace mortality_opiates2=1 if racs`i'_11=="Y" & racs`i'_22==35

replace mortality_opiates2=1 if racs`i'_11=="T" & racs`i'_23==401
replace mortality_opiates2=1 if racs`i'_11=="T" & inrange(racs`i'_23,400,404)
replace mortality_opiates2=1 if racs`i'_11=="T" & racs`i'_23==406
replace mortality_opiates2=1 if racs`i'_11=="F" & racs`i'_22==11

replace mortality_opiates2=1 if racs`i'_11=="F" & racs`i'_22==19

replace mortality_opiates2=1 if racs`i'_11=="R" & racs`i'_22==99
}

********************************************************************************
*** all drugs - narrow
cap drop drugs
gen drugs=0
replace drugs=1 if mortality_opiates==1

replace drugs=1 if ICD10_1=="X" & ICD10_2==46
replace drugs=1 if ucod_recode=="O355" 
replace drugs=1 if ucod_recode=="P044" 
replace drugs=1 if ucod_recode=="P961"
replace drugs=1 if ucod_recode=="T387"
replace drugs=1 if ICD10_1=="T" & ICD10_2==40
replace drugs=1 if ucod_recode=="T436"
replace drugs=1 if ICD10_1=="Y" & ICD10_2==16

replace drugs=1 if ucod_recode=="T509"

replace drugs=1 if ICD10_1=="B" & ICD10_2==17
replace drugs=1 if ICD10_1=="B" & ICD10_2==94

*** all drugs - broad
cap drop drugs2
gen drugs2=0
replace drugs2=1 if mortality_opiates2==1

forvalues i=1/12 {

replace drugs2=1 if racs`i'_11=="X" & racs`i'_22==46
replace drugs2=1 if racs`i'=="O355" 
replace drugs2=1 if racs`i'=="P044" 
replace drugs2=1 if racs`i'=="P961"
replace drugs2=1 if racs`i'=="T387"
replace drugs2=1 if racs`i'_11=="T" & racs`i'_22==40
replace drugs2=1 if racs`i'=="T436"
replace drugs2=1 if racs`i'_11=="Y" & racs`i'_22==16

* additional to Evans and Moore, but used for drugs (adds 6 cases)
replace drugs2=1 if racs`i'=="T509"

* adding sequeale for hepatitis
replace drugs2=1 if racs`i'_11=="B" & racs`i'_22==17
replace drugs2=1 if racs`i'_11=="B" & racs`i'_22==94
}

********************************************************************************
*** all drugs  and alcohol - narrow
cap drop drugsalc
gen drugsalc=0
replace drugsalc=1 if drugs==1

replace drugsalc=1 if ICD10_1=="F" & ICD10_2==10
replace drugsalc=1 if ucod_recode=="G312" 
replace drugsalc=1 if ucod_recode=="G621" 
replace drugsalc=1 if ucod_recode=="G721" 
replace drugsalc=1 if ucod_recode=="I426" 
replace drugsalc=1 if ucod_recode=="K292" 
replace drugsalc=1 if ICD10_1=="K" & ICD10_2==70
replace drugsalc=1 if ICD10_1=="K" & ICD10_2==73
replace drugsalc=1 if ICD10_1=="K" & ICD10_2==74
replace drugsalc=1 if ucod_recode=="K860" 
replace drugsalc=1 if ucod_recode=="O354" 
replace drugsalc=1 if ucod_recode=="P043" 
replace drugsalc=1 if ucod_recode=="Q860" 
replace drugsalc=1 if ucod_recode=="T510" 
replace drugsalc=1 if ucod_recode=="T511" 
replace drugsalc=1 if ucod_recode=="T519" 
replace drugsalc=1 if ucod_recode=="X45" 
replace drugsalc=1 if ucod_recode=="Y15" 

*** all drugs and alcohol - broad
cap drop drugsalc2
gen drugsalc2=0
replace drugsalc2=1 if drugs2==1

forvalues i=1/12 {
replace drugsalc2=1 if racs`i'_11=="F" & racs`i'_22==10

replace drugsalc2=1 if racs`i'=="G312"
replace drugsalc2=1 if racs`i'=="G621" 
replace drugsalc2=1 if racs`i'=="G721"
replace drugsalc2=1 if racs`i'=="I426"
replace drugsalc2=1 if racs`i'=="K292"
replace drugsalc2=1 if racs`i'_11=="K" & racs`i'_22==70
replace drugsalc2=1 if racs`i'_11=="K" & racs`i'_22==73
replace drugsalc2=1 if racs`i'_11=="K" & racs`i'_22==74
replace drugsalc2=1 if racs`i'=="K860"
replace drugsalc2=1 if racs`i'=="O354"
replace drugsalc2=1 if racs`i'=="P043"
replace drugsalc2=1 if racs`i'=="Q860"
replace drugsalc2=1 if racs`i'=="T510"
replace drugsalc2=1 if racs`i'=="T511"
replace drugsalc2=1 if racs`i'=="T519"
replace drugsalc2=1 if racs`i'=="X45"
replace drugsalc2=1 if racs`i'=="Y15"
}

keep ppn ym mortality mortality_opiates mortality_opiates2 drugs drugs2 	///
	drugsalc drugsalc2 ucod_recode record_axis_count 

tempfile icd10
save `icd10', replace

*** combining the two files together 
use `icd10', clear
append using `icd9'

rename drugs mortality_drugs 
rename drugsalc mortality_drugsalc 
rename drugs2 mortality_drugs2 
rename drugsalc2 mortality_drugsalc2 

save "data/icd.dta", replace



/********************************************************************************
* Create TREATMENT and CONTROL groups
*******************************************************************************/
***
*** treated group based on opioid offense drug user
***
*start with individuals who had a opioid drug offense prior to Q4 2000
use  "data/offense_ym.dta", clear
drop if ym>ym(2000,9)
gen off_drug_use=off_drug_opiates_use
keep if off_drug_use>0

sort ppn ym
gen ym_drug_off=ym if off_drug_use>0 & off_drug~=.
bys ppn: egen first_drug_off_ym = min(ym_drug_off)
drop ym_drug_off

keep ppn first_drug_off_ym
duplicates drop 
duplicates tag ppn, gen(dup)
tab dup

save "data/estsample1.dta", replace

***
***control group based on non-opioid drug user
***
*start with individuals who had a non-opioid drug offense prior to Q4 2000
use  "data/offense_ym.dta", clear
drop if ym>ym(2000,9)
gen off_drug_use=off_drug_amph_use+off_drug_cocaine_use+off_drug_other_use+off_drug_cannabis_use+off_drug_missing_use
keep if off_drug_use>0

sort ppn ym
gen ym_drug_off=ym if off_drug_use>0 & off_drug~=.
bys ppn: egen first_drug_off_ym = min(ym_drug_off)
drop ym_drug_off

keep ppn first_drug_off_ym
duplicates drop 
duplicates tag ppn, gen(dup)
tab dup

*remove those who participated in opioid treatment program 
preserve 
use "data/phdas_ym.dta", clear
keep ppn
duplicates drop
tempfile ppn
save `ppn'
restore

merge 1:1 ppn using `ppn'
drop if (_merge==2 | _merge==3) 
drop _merge

*remove those who had an opioid-related offense prior to supply shock
preserve 
use  "data/offense_ym.dta", clear
keep if off_drug_opiates>0 & off_drug_opiates~=. & ym<=ym(2000,12)
keep ppn 
duplicates drop
tempfile opoff
save `opoff'
restore

merge 1:1 ppn using `opoff'
drop if (_merge==2 | _merge==3) 
drop _merge

*drop if first offense is later than sept. 2000
preserve 
use  "data/offense_ym.dta", clear
sort ppn ym
bys ppn: egen off_first_ym=min(ym)
keep ppn off_first_ym
duplicates drop
tempfile off
save `off'
restore

merge 1:1 ppn using `off'
drop if _merge==2
drop _merge

drop if off_first_ym>ym(2000,9) 

save "data/estsample0.dta", replace


***
*** create panel datasets (ppn by yq) for treatment and control
***
forvalues i=0/1 {

use "data/mortality_ym.dta", clear
gen mortality_ym=ym
keep ppn mortality_ym
duplicates drop
tempfile mort
save `mort'

use "data/estsample`i'.dta", clear

merge 1:1 ppn using `mort'
drop if _merge==2
drop _merge

gen ym_beg=first_drug_off_ym + 1
replace ym_beg=ym(1994,1) if first_drug_off_ym<ym(1994,1)

gen ym_end=mortality_ym if mortality_ym~=.
replace ym_end=ym(2008,12) if ym_end==.

keep ppn ym_beg ym_end
tempfile begend
save `begend', replace

reshape long ym_, i(ppn) j(j) string
rename ym_ ym
bys ppn: drop if ym==ym[_n-1] & j=="end"
xtset ppn ym

tsfill
drop j

merge 1:1 ppn ym using "data/phdas_ym.dta"
drop if _merge==2
drop _merge
merge 1:1 ppn ym using "data/offense_ym.dta"
drop if _merge==2
drop _merge
merge 1:1 ppn ym using "data/mortality_ym.dta"
drop if _merge==2
drop _merge
merge m:1 ppn using "data/demographic.dta"
drop if _merge==2
drop _merge
merge m:1 ppn using "data/estsample`i'.dta"
drop if _merge==2
drop _merge

foreach var of varlist mortality mortality_overdose* mortality_opiates* mortality_heroin mortality_drugs* mortality_cancer mortality_heart mortality_external mortality_accidental_drug mortality_accidental_injury mortality_suicide mortality_violence off_* phdas_beg_ym phdas_new_ym phdas_end_ym phdas_end_ym_earlydropout phdas_beg_ym_days_on phdas_days_ym  n_phdas_episodes_ym {
replace `var'=0 if `var'==.
}

keep if inrange(ym,ym(1994,1),ym(2008,12))

drop mortality_ucod* mortality_sa2_2011

gen yq=yq(year(dofm(ym)),quarter(dofm(ym)))

gen age=trunc((ym-ym(d_birthyear,d_birthmonth))/12)
replace age=year(dofm(ym))-d_birthyear if d_birthyear!=. & age==.

gen female=d_sex==2
gen female_missing=d_sex==.
gen indig=d_indig_bocsar==1
gen indig_missing=d_indig_bocsar==.

*create dummy variables for different types of leaving
gen phdas_end_leaving_progfail=phdas_end_leaving==23
gen phdas_end_leaving_progsuccess=phdas_end_leaving==22
gen phdas_end_leaving_cleanup=phdas_end_leaving==90

capture drop off_drug_use
gen off_drug_use_w_missing=off_drug_amph_use+off_drug_cocaine_use+off_drug_other_use+off_drug_cannabis_use+off_drug_missing_use

collapse (sum) off_* phdas_days_yq=phdas_days_ym  n_phdas_episodes_ym (first) ym   (max)  phdas_end_leaving_* phdas_new_yq=phdas_new_ym phdas_end_yq=phdas_end_ym phdas_beg_yq=phdas_beg_ym phdas_end_yq_earlydropout=phdas_end_ym_earlydropout mortality* age* female female_missing indig indig_missing  first_*_ym , by(ppn yq)

rename n_phdas_episodes_ym n_phdas_episodes_yq

foreach var of varlist off_* {
gen `var'_ext=(`var'>0 & `var'~=.)
}

gen phdas_yq=(phdas_days_yq>0 & phdas_days_yq~=.)

gen year=year(dofq(yq))

drop mortality_overdose1 mortality_overdose2 mortality_heroin mortality_opiates1 ///
 mortality_opiates2 mortality_opiates3 mortality_drugs mortality_drugsalc ///
 mortality_accidental_drug

sort ppn mortality

merge m:1 ppn mortality using "data/icd.dta"
drop if _merge==2

keep if year>=1994
keep if year<2009

replace mortality_opiates=0 if mortality_opiates==. 
replace mortality_opiates2=0 if mortality_opiates2==. 
replace mortality_drugs=0 if mortality_drugs==. 
replace mortality_drugs2=0 if mortality_drugs2==. 
replace mortality_drugsalc=0 if mortality_drugsalc==. 
replace mortality_drugsalc2=0 if mortality_drugsalc2==. 

gen mortality_otherdrugs=mortality_drugs
replace mortality_otherdrugs=0 if mortality_opiates==1
gen mortality_nondrugs=mortality
replace mortality_nondrugs=0 if mortality_drugs==1

gen mortality_otherdrugsalc=mortality_drugsalc
replace mortality_otherdrugsalc=0 if mortality_opiates==1
gen mortality_nondrugsalc=mortality
replace mortality_nondrugsalc=0 if mortality_drugsalc==1

gen mortality_otherdrugs2=mortality_drugs2
replace mortality_otherdrugs2=0 if mortality_opiates2==1
gen mortality_nondrugs2=mortality
replace mortality_nondrugs2=0 if mortality_drugs2==1

gen mortality_otherdrugsalc2=mortality_drugsalc2
replace mortality_otherdrugsalc2=0 if mortality_opiates2==1
gen mortality_nondrugsalc2=mortality
replace mortality_nondrugsalc2=0 if mortality_drugsalc2==1

preserve
keep ppn first_drug_off_ym
drop if first_drug_off_ym==.
duplicates drop
capture drop _merge
merge 1:1 ppn using "data/demographic.dta"
drop if _merge~=3
drop _merge
gen age_first_drug_off=year(dofm(first_drug_off_ym))-d_birthyear
keep ppn age_first_drug_off
tempfile age2
save `age2'
restore

capture drop _merge
merge m:1 ppn using `age2'
drop if _merge==2

save "data/panel`i'.dta", replace
}

* combine treatment and control into one panel dataset 
use "data/panel1.dta", clear
gen treated=1
append using "data/panel0.dta"
replace treated=0 if treated==.

gen age_at_entry=age_first_drug_off 

sort ppn yq
xtset ppn yq

tab year

keep if inrange(yq,yq(1994,1),yq(2008,4))

drop if age==.

tsegen ageb=rowmean(F(0/3).age)
tsegen agec=rowmean(L(1/4).age) 
replace agec= agec + 1
egen maxage=max(age), by(ppn)

gen aged=ageb
replace aged=agec if age==maxage

drop age 
rename aged age 
gen age2=age*age
gen age3=age*age2
gen age4=age*age3

*year dummies - treatment and control
forvalues q=1994/2008 {
gen year_`q'=(year==`q')
gen tyear_`q'=(year==`q' & treated==1)
	}

*qtr in final qtr of 2000
gen y2000q4=(yq==163)
gen t2000q4=(y2000q4==1 & treated==1)

gen qtr=(1960+yq/4-year)*4

format yq %tq

*non-mv theft
gen off_theft_nonmv=off_theft - off_mv_theft
summ off_theft_nonmv

*non-robbery violent
gen off_violent_nonrobb=off_violent - off_robbery
summ off_violent_nonrobb

*property including robbery
gen off_property_robb=off_property + off_robbery
summ off_property_robb

*total assault
gen off_assault_total=off_assault + off_sexassault
summ off_assault_total

*remainder for violent
gen off_violent_remain=off_violent - off_robbery - off_assault - off_sexassault - off_murder_plus

*** Drug selling
*any "hard" non-opioid drug dealing
gen off_drug_nonopioid_sale=off_drug_amph_sale + off_drug_cocaine_sale 
gen off_drug_nonopioid_sale2=off_drug_amph_sale + off_drug_cocaine_sale + off_drug_other_sale 
gen off_drug_nonopioid_sale3=off_drug_amph_sale + off_drug_cocaine_sale + off_drug_other_sale + off_drug_cannabis_sale
*any "hard" drug dealing
gen off_drug_hard_sale=off_drug_amph_sale + off_drug_cocaine_sale + off_drug_opiates_sale 
gen off_drug_hard_sale2=off_drug_amph_sale + off_drug_cocaine_sale + off_drug_opiates_sale + off_drug_other_sale 
*any drug dealing
gen off_drug_any_sale=off_drug_amph_sale + off_drug_cocaine_sale + off_drug_other_sale + off_drug_cannabis_sale + off_drug_opiates_sale

*** Drug use
*any "hard" non-opioid drug dealing
gen off_drug_nonopioid_use=off_drug_amph_use + off_drug_cocaine_use 
gen off_drug_nonopioid_use2=off_drug_amph_use + off_drug_cocaine_use + off_drug_other_use 
gen off_drug_nonopioid_use3=off_drug_amph_use + off_drug_cocaine_use + off_drug_other_use + off_drug_cannabis_use
*any "hard" drug dealing
gen off_drug_hard_use=off_drug_amph_use + off_drug_cocaine_use + off_drug_opiates_use 
gen off_drug_hard_use2=off_drug_amph_use + off_drug_cocaine_use + off_drug_opiates_use + off_drug_other_use 
*any drug dealing
gen off_drug_any_use=off_drug_amph_use + off_drug_cocaine_use + off_drug_other_use + off_drug_cannabis_use + off_drug_opiates_use


gen yq_beg=qofd(dofm(first_drug_off_ym)) 
gen year_beg=year(dofq(yq_beg))

sort ppn yq
gen year_beg_off_temp=year if off_all>=1
bys ppn: egen year_beg_off=min(year_beg_off_temp)
drop year_beg_off_temp

gen age_int=floor(age)

*clean up OTP 

preserve 

capture drop _merge
keep ppn yq treated

merge 1:1 ppn yq using "data/otp_yq.dta"
drop if _merge==2 

foreach var of varlist otp_* {
replace `var'=0 if _merge==1
}
replace otp_out=1 if _merge==1

drop _merge
*keep if treated==1

bys ppn: carryforward first last, replace

*** Maximum days in the quarter
sort yq
merge m:1 yq using "data/otp_days_max.dta"
keep if _merge==3
drop _merge
sort ppn
***

*** Entries
gen otp_entry=(otp_entries>0) 
replace otp_reentries=. if otp_out==0
gen otp_reentry=(otp_reentries>0)
replace otp_reentry=. if otp_out==0
*replace otp_reentry=. if otp_days_yq>=90

*** Exits
gen otp_exit=(otp_exits>0)
replace otp_exit=. if otp_days_yq==0
gen otp_exit_success=(otp_exits_success>0)
replace otp_exit_success=. if otp_days_yq==0
gen otp_exit_failure=(otp_exits_failure>0)
replace otp_exit_failure=. if otp_days_yq==0

**OTP data had a systematic exit in the first quarter of 1999, we think this was 
* an administrative/coding issue

gen adminexit=(otp_exit==1 & yq==yq(1999,1))
bys ppn: egen admindummy=max(adminexit)


foreach var of varlist otp_exit* {
replace `var'=. if admindummy==1
}

foreach var of varlist otp_exit* {
 gen R`var'=`var'*1000*4
}

foreach var of varlist otp_reent* {
 gen R`var'=`var'*1000*4
}

tempfile otpnew
save `otpnew'
restore


capture drop _merge
merge 1:1 ppn yq using `otpnew'
foreach var of varlist *otp_* {
replace `var'=0 if _merge==1
}
drop _merge


* create year variables relative to shock

keep if inrange(yq,yq(1994,4),yq(2008,4))

*keep track of years from Q3 through Q4 prior to shortage
gen Ayear=.
forvalues q=1995/2000 {
replace Ayear=`q' if ((year==`q' & 	inrange(quarter(dofq(yq)),1,3)) | (year==`q'-1 & quarter(dofq(yq))==4))
}

replace Ayear=2000 if yq==yq(2000,4)

forvalues q=2001/2008 {
replace Ayear=`q' if year==`q'
}
 
gen Atyear=.
replace Atyear=Ayear if treated==1

forvalues q=1995/2008 {
gen Ayear_`q'=(Ayear==`q')
gen Atyear_`q'=(Atyear==`q')
} 

gen q4y2000=yq==yq(2000,4)
gen q4y2000_T=(yq==yq(2000,4) & treated==1)


*code if in control group and have a missing drug type offense (could be opiate)
preserve 
use  "data/offense_ym.dta", clear
keep if off_drug_missing>0 & off_drug_missing~=. & ym<=ym(2000,12)
keep ppn 
duplicates drop
tempfile missoff
save `missoff'
restore

merge m:1 ppn using `missoff'
drop if _merge==2
gen control_drug_missing=(_merge==3 & treated==0)
drop _merge

preserve 
keep first_drug_off_ym ppn treated 
duplicates drop 
tempfile first
save `first'

use "data/offense_ym.dta", clear

*non-mv theft
gen off_theft_nonmv=off_theft - off_mv_theft
summ off_theft_nonmv

*non-robbery violent
gen off_violent_nonrobb=off_violent - off_robbery
summ off_violent_nonrobb

*property including robbery
gen off_property_robb=off_property + off_robbery
summ off_property_robb

*total assault
gen off_assault_total=off_assault + off_sexassault
summ off_assault_total

*remainder for violent
gen off_violent_remain=off_violent - off_robbery - off_assault - off_sexassault - off_murder_plus

*** Drug selling
*any "hard" non-opioid drug dealing
gen off_drug_nonopioid_sale=off_drug_amph_sale + off_drug_cocaine_sale 
gen off_drug_nonopioid_sale2=off_drug_amph_sale + off_drug_cocaine_sale + off_drug_other_sale 
gen off_drug_nonopioid_sale3=off_drug_amph_sale + off_drug_cocaine_sale + off_drug_other_sale + off_drug_cannabis_sale
*any "hard" drug dealing
gen off_drug_hard_nonO_sale=off_drug_amph_sale + off_drug_cocaine_sale + off_drug_opiates_sale 
gen off_drug_hard_nonO_sale2=off_drug_amph_sale + off_drug_cocaine_sale + off_drug_opiates_sale + off_drug_other_sale 
*any drug dealing
gen off_drug_any_sale=off_drug_amph_sale + off_drug_cocaine_sale + off_drug_other_sale + off_drug_cannabis_sale + off_drug_opiates_sale

*** Drug use
*any "hard" non-opioid drug dealing
gen off_drug_nonopioid_use=off_drug_amph_use + off_drug_cocaine_use 
gen off_drug_nonopioid_use2=off_drug_amph_use + off_drug_cocaine_use + off_drug_other_use 
gen off_drug_nonopioid_use3=off_drug_amph_use + off_drug_cocaine_use + off_drug_other_use + off_drug_cannabis_use
*any "hard" drug dealing
gen off_drug_hard_nonO_use=off_drug_amph_use + off_drug_cocaine_use + off_drug_opiates_use 
gen off_drug_hard_nonO_use2=off_drug_amph_use + off_drug_cocaine_use + off_drug_opiates_use + off_drug_other_use 
*any drug dealing
gen off_drug_any_use=off_drug_amph_use + off_drug_cocaine_use + off_drug_other_use + off_drug_cannabis_use + off_drug_opiates_use

keep off_* ppn ym

merge m:1 ppn using `first'
drop if _merge==1

*gen info about first drug offense / initiation offense

foreach var of varlist off_drug* {
	gen first_`var'=`var' if ym==first_drug_off_ym
}


keep if ym<=first_drug_off_ym

foreach var of varlist off* {
	gen pre_`var'=`var'
}

collapse (first) first_* treated (sum) pre_off_*, by(ppn)

tempfile first2
save `first2'
restore


merge m:1 ppn using `first2'
drop _merge
 

/*
SAMPLE RESTRICTIONS
*/
*limit age range to 15-65
keep if inrange(age,15,65) 
*drop if drug type is missing for initiation offense
drop if control_drug_missing==1
*restrict to entry before October 2000
keep if first_drug_off_ym<=ym(2000,9)
* limit years 1996-2008
drop Ayear_2000 Atyear_2000
keep if inrange(Ayear,1996,2008)


/*
NEW VARIABLES
*/

*time of entry
gen entry_yq=yq(year(dofm(first_drug_off_ym)),quarter(dofm(first_drug_off_ym)))
format entry_yq %tq
gen entry_year=year(dofm(first_drug_off_ym))

*nonopioid mortality 
gen mortality_nonopioid = mortality_otherdrugsalc + mortality_nondrugsalc

*non opioid drug offending
gen off_drug_hard_nonO_use=off_drug_amph_use+off_drug_cocaine_use+off_drug_other_use
gen off_drug_hard_nonO_sale=off_drug_amph_sale+off_drug_cocaine_sale+off_drug_other_sale
gen off_drug_hard_nonO=off_drug_hard_nonO_use+off_drug_hard_nonO_sale

*types of property
gen off_property_serious=off_breakenter+off_mv_theft+off_robbery

*types of violent
gen off_assault_serious=off_assault_seriousinj+off_assault_seriousnoninj
gen off_violent_other=off_violent_nonrobb - off_assault - off_sexassault - off_harrassment
gen off_violent_other2 = off_violent_nonrobb - off_assault - off_sexassault - off_murder_plus
gen off_violent_other3 = off_domestic + off_abduction + off_harrassment

*violent and property
gen off_violprop_serious=off_violent_nonrobb+off_property_serious
*violent crime and drug crime in same quarter
gen off_violdrug_ext=(off_violent_nonrobb>0 & off_drug_hard_nonO_use>0)
*property serious crime and drug crime in same quarter
gen off_propdrug_ext=(off_property_serious>0 & off_drug_hard_nonO_use>0)

gen off_driving=off_driving_alcohol+off_driving_influence

*save "$clean/panelB_clean_Nov2022.dta", replace
save "data/panel.dta", replace
}



*******************************************************************************
*** CODE FOR ALL TABLES AND FIGURES 
*******************************************************************************

*** NSW population
import delimited "data/nsw_pop.csv", clear
drop if year == .
keep if year>=1995 & year<=2015
tab year

gen yq=yq(year,quarter)
sort yq

tempfile nswpop
save `nswpop'

*save "$bocsar\nsw_pop.dta", replace

*** NSW crimes
import delimited "data/nsw_incidents.csv", clear
drop if year == .
keep if year>=1995 & year<=2015

label variable homicide1 		"Homicide Attempted murder"
label variable homicide2 		"Homicide Murder accessory, conspiracy"
label variable homicide3 		"Homicide Manslaughter"
label variable homicide4 		"Homicide Murder"
label variable assault1			"Assault Domestic violence related assault"
label variable assault2			"Assault Non-domestic violence related assault"
label variable assault3			"Assault Assault Police"
label variable sex1				"Sexual offences Sexual assault"
label variable sex2				"Sexual offences Indecent assault, act of indecency and other sexual offences"
label variable abduction1		"Abduction and kidnapping"
label variable robbery1			"Robbery	Robbery without a weapon"
label variable robbery2			"Robbery	Robbery with a firearm"
label variable robbery3			"Robbery	Robbery with a weapon not a firearm"
label variable blackmail1		"Blackmail and extortion"	
label variable other_person1	"Other offences against the person"
label variable theft1			"Theft	Break and enter dwelling"
label variable theft2			"Theft	Break and enter non-dwelling"
label variable theft3			"Theft	Receiving or handling stolen goods"
label variable theft4			"Theft	Motor vehicle theft"
label variable theft5			"Theft	Steal from motor vehicle"
label variable theft6			"Theft	Steal from retail store"
label variable theft7			"Theft	Steal from dwelling"
label variable theft8			"Theft	Steal from person"
label variable theft9			"Theft	Stock theft"
label variable theft10			"Theft	Fraud"
label variable theft11			"Theft	Other theft"
label variable arson1			"Arson"
label variable prop_damage1		"Malicious damage to property"
label variable drug1			"Drug offences	Possession and/or use of cocaine"
label variable drug2			"Drug offences	Possession and/or use of narcotics"
label variable drug3			"Drug offences	Possession and/or use of cannabis"
label variable drug4			"Drug offences	Possession and/or use of amphetamines"
label variable drug5			"Drug offences	Possession and/or use of other drugs"
label variable drug6			"Drug offences	Dealing, trafficking in cocaine"
label variable drug7			"Drug offences	Dealing, trafficking in narcotics"
label variable drug8			"Drug offences	Dealing, trafficking in cannabis"
label variable drug9			"Drug offences	Dealing, trafficking in amphetamines"
label variable drug10			"Drug offences	Dealing, trafficking in other drugs"
label variable drug11			"Drug offences	Cultivating cannabis"
label variable drug12			"Drug offences	Manufacture drug"
label variable drug13			"Drug offences	Importing drugs"
label variable drug14			"Drug offences	Other drug offences"
label variable drug15			"Drug offences	Dealing, trafficking in ecstasy"
label variable drug16			"Drug offences	Possession and/or use of ecstasy"
label variable weapons1			"Prohibited and regulated weapons offences"
label variable disorderly1		"Disorderly conduct	Trespass"
label variable disorderly2		"Disorderly conduct	Offensive conduct"
label variable disorderly3		"Disorderly conduct	Offensive language"
label variable disorderly4		"Disorderly conduct	Criminal intent"
label variable betting1			"Betting and gaming offences"	
label variable liquor1			"Liquor offences"
label variable pornography1		"Pornography offences"
label variable prostitution1	"Prostitution offences"
label variable justice1			"Against justice procedures	Escape custody"
label variable justice2			"Against justice procedures	Breach Apprehended Violence Order"
label variable justice3			"Against justice procedures	Breach bail conditions"
label variable justice4			"Against justice procedures	Fail to appear"
label variable justice5			"Against justice procedures	Resist or hinder officer"
label variable justice6			"Against justice procedures	Other offences against justice procedures"
label variable transport1		"Transport regulatory offences"	
label variable other1			"Other offences"
label variable harassment1 		"Intimidation, stalking and harassment"

gen yq=yq(year,quarter)

sort yq
collapse (sum) homicide1-homicide4 assault1-assault3 sex1 sex2 abduction1		///
 robbery1-robbery3 blackmail1 other_person1 theft1-theft11 arson1 prop_damage1	///
 drug1-drug16 weapons1 disorderly1-disorderly4 betting1 liquor1 pornography1	///
 prostitution1 justice1-justice6 transport1 other1 harassment1, by(yq)

merge 1:1 yq using `nswpop'
drop _merge

save "data/nsw_incidents.dta", replace



use "data/nsw_incidents.dta", clear //data created by "incident_analysis.do"  
*label variable drug2			"Drug offences	Possession and/or use of narcotics"
*label variable drug7			"Drug offences	Dealing, trafficking in narcotics"

collapse (sum) drug2 drug7 (mean) pop , by(year)

gen possession_narcotics_p10k=(drug2/pop)*10000
gen dealing_narcotics_p10k=(drug2/pop)*10000
gen narcotics_p10k=possession_narcotics_p10k+dealing_narcotics_p10k

keep *narcotics* year
tempfile total
save `total'
  
insheet using "data/price_heroin_cocaine_meth.csv", clear

gen her_price=her_dose*inflate_all/her_pur_all*100
keep her_price year

tempfile p2
save `p2'

*insheet using "$databackground/NSW_opioid_death_rate_data.csv", clear
import delimited "data/us_nsw_opioids_comparison.csv", clear 

gen year=nsw_year

merge 1:1 year using `p2'
drop _merge
merge 1:1 year using `total'

drop if year>2008
drop if year<1994


 twoway  line nsw_deathr year, lc(black) lw(thick)  || ///
 line her_price year, lcolor(black) lp(dash) lw(thick) yaxis(2)  ///
	, xlabel(1994(2)2008) yscale(range(0 8)) ylabel(0(2)8, angle(horizontal)) ylabel(0(100)450, angle(horizontal) axis(2)) yscale(range(0 450) axis(2)) ///
	scheme(s1mono) xsc(titlegap(2)) xtitle("Year") ytitle( "Purity-adjusted dose price ($)", axis(2)) ///
	ytitle("Deaths per 100,000 pop.") ysc(titlegap(2)) ysc(titlegap(0) axis(2))  ///
	legend(order(1  "Mortality rate (on left)" 2 "Heroin price (on right)") cols(1) ring(0) position(5)) ///
	xsize(6)

graph export "results/Figure1.pdf", replace
 
 
 
********************************************************************************
********************************************************************************
*1. Figure 2 
********************************************************************************
********************************************************************************

*Raw data

use "data/panel.dta", clear

global depvars "mortality mortality_opiates2 mortality_nonopioid"

foreach var of varlist $depvars {

preserve 
collapse (sum) `var' (count) obs=ppn, by(yq treated)
gen R`var'=1000*4*(`var'/obs)
keep R`var' yq treated
reshape wide R`var', i(yq) j(treated)
format yq %tq

cap drop fakeline
gen fakeline=.

drop if yq<=yq(1995,3)

twoway (line R`var'1 yq, lc(black) lp(solid) lw(thick)) (line R`var'0 yq, lc(gs4) lp(shortdash) lw(thick)) ///
(scatter fakeline yq, mc(red) msymbol(|) msize(large) ) ///
,  xline(`=yq(2000,4)', lc(red)  lw(thick)   ) scheme(s1mono) ///
ytitle("Rate per 1000") ylabel(, angle(horizontal) format(%9.0fc)) ///
xtitle("")  xlabel(`=yq(1996,4)'(8)`=yq(2008,1)') ///
 xtick(`=yq(1996,4)'(4)`=yq(2008,1)') ///
  legend(off) 
graph export "results/Figure2_RAW_`var'.pdf", replace 
 
restore
}

*Event study

global depvarsnofe "mortality mortality_opiates2 mortality_nonopioid "

global indcontrols "treated i.female i.indig i.female_missing i.indig_missing  "
global agecontrols "i.age_int  i.age_int#i.female i.age_int#i.indig "
global offcontrols "i.first_drug_off_ym"

foreach var of varlist $depvarsnofe {
	
cap drop Ayear_2000 Atyear_2000

capture drop R`var' 
gen R`var'=`var'*1000*4

tempfile coeff
save `coeff'

reg R`var' $indcontrols $agecontrols $offcontrols treated ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr  , vce(cluster ppn)
regsave using `coeff', addvar(Atyear_2000,0,0) ci replace

preserve
use `coeff', clear
keep var coef ci_lower ci_upper
gen year=substr(var,8,4)
gen grp=substr(var,1,2)
destring year, replace force
keep if grp=="At"
keep if year>=1995 & year<=2008
replace ci_lower=0 if year==2000
replace ci_upper=0 if year==2000
drop var
sort year
gen time=year - 2001 

rename coef E`var'
rename ci_lower E`var'_l
rename ci_upper E`var'_u

cap drop fakeline
gen fakeline=.

*event study
twoway (line E`var' time, lc(black) lp(solid) lw(medthick)) ///
 (rarea E`var'_l E`var'_u time, color(gs12%50) lp(shortdash) lc(black) ) ///
 (scatter fakeline time, mc(red) msym(|)) ///
 , yline(0) xline(-1, lc(red)) scheme(s1mono) ///
 xlabel(-5(1)7) xtitle("Years (relative to onset)") xsc(titlegap(1)) ///
 ylabel(, angle(horizontal)) ytitle("Estimates") ysc(titlegap(0)) ///
 legend(off)  
 graph export "results/Figure2_ES_`var'.pdf", replace 


restore
}


 
 ********************************************************************************
********************************************************************************
*Figure 3 
********************************************************************************
********************************************************************************

*Raw data

use "data/panel.dta", clear

global depvars "off_drug_hard_nonO_use  off_drug_cocaine_use  off_drug_amph_use"

foreach var of varlist $depvars {

preserve 
collapse (sum) `var' (count) obs=ppn, by(yq treated)
gen R`var'=1000*4*(`var'/obs)
keep R`var' yq treated
reshape wide R`var', i(yq) j(treated)
format yq %tq

cap drop fakeline
gen fakeline=.

drop if yq<=yq(1995,3)

twoway (line R`var'1 yq, lc(black) lp(solid) lw(thick)) (line R`var'0 yq, lc(gs4) lp(shortdash) lw(thick)) ///
(scatter fakeline yq, mc(red) msymbol(|) msize(large) ) ///
,  xline(`=yq(2000,4)', lc(red)  lw(thick)   ) scheme(s1mono) ///
ytitle("Rate per 1000") ylabel(, angle(horizontal) format(%9.0fc)) ///
xtitle("")  xlabel(`=yq(1996,4)'(8)`=yq(2008,1)') ///
 xtick(`=yq(1996,4)'(4)`=yq(2008,1)') ///
  legend(off) 
graph export "results/Figure3_RAW_`var'.pdf", replace 
 
restore
}

*Event study

global depvarsfe " off_drug_hard_nonO_use off_drug_cocaine_use  off_drug_amph_use "

global indcontrols "treated i.female i.indig i.female_missing i.indig_missing  "
global agecontrols "i.age_int  i.age_int#i.female i.age_int#i.indig "
global offcontrols "i.first_drug_off_ym"

foreach var of varlist $depvarsfe {
	
cap drop Ayear_2000 Atyear_2000

capture drop R`var' 
gen R`var'=`var'*1000*4

tempfile coeff
save `coeff'

areg R`var' $indcontrols $agecontrols $offcontrols ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr  , absorb(ppn) vce(cluster ppn)
regsave using `coeff', addvar(Atyear_2000,0,0) ci replace

preserve
use `coeff', clear
keep var coef ci_lower ci_upper
gen year=substr(var,8,4)
gen grp=substr(var,1,2)
destring year, replace force
keep if grp=="At"
keep if year>=1995 & year<=2008
replace ci_lower=0 if year==2000
replace ci_upper=0 if year==2000
drop var
sort year
gen time=year - 2001 

rename coef E`var'
rename ci_lower E`var'_l
rename ci_upper E`var'_u

cap drop fakeline
gen fakeline=.

*event study
twoway (line E`var' time, lc(black) lp(solid) lw(medthick)) ///
 (rarea E`var'_l E`var'_u time, color(gs12%50) lp(shortdash) lc(black) ) ///
 (scatter fakeline time, mc(red) msym(|)) ///
 , yline(0) xline(-1, lc(red)) scheme(s1mono) ///
 xlabel(-5(1)7) xtitle("Years (relative to onset)") xsc(titlegap(1)) ///
 ylabel(, angle(horizontal)) ytitle("Estimates") ysc(titlegap(0)) ///
 legend(off)  
 graph export "results/Figure3_ES_`var'.pdf", replace 

restore
}



********************************************************************************
********************************************************************************
*1. Figure 4 
********************************************************************************
********************************************************************************

*Raw data

use "data/panel.dta", clear

global depvars "off_murder_plus off_assault_serious off_assault_common off_sexassault"

foreach var of varlist $depvars {

preserve 
collapse (sum) `var' (count) obs=ppn, by(yq treated)
gen R`var'=1000*4*(`var'/obs)
keep R`var' yq treated
reshape wide R`var', i(yq) j(treated)
format yq %tq

cap drop fakeline
gen fakeline=.

drop if yq<=yq(1995,3)

twoway (line R`var'1 yq, lc(black) lp(solid) lw(thick)) (line R`var'0 yq, lc(gs4) lp(shortdash) lw(thick)) ///
(scatter fakeline yq, mc(red) msymbol(|) msize(large) ) ///
,  xline(`=yq(2000,4)', lc(red)  lw(thick)   ) scheme(s1mono) ///
ytitle("Rate per 1000") ylabel(, angle(horizontal) format(%9.0fc)) ///
xtitle("")  xlabel(`=yq(1996,4)'(8)`=yq(2008,1)') ///
 xtick(`=yq(1996,4)'(4)`=yq(2008,1)') ///
  legend(off) 
graph export "results/Figure4_RAW_`var'.pdf", replace 
 
restore
}

*Event study

global depvarsfe "off_murder_plus off_assault_serious off_assault_common off_sexassault"

global indcontrols "treated i.female i.indig i.female_missing i.indig_missing  "
global agecontrols "i.age_int  i.age_int#i.female i.age_int#i.indig "
global offcontrols "i.first_drug_off_ym"

foreach var of varlist $depvarsfe {
	
cap drop Ayear_2000 Atyear_2000

capture drop R`var' 
gen R`var'=`var'*1000*4

tempfile coeff
save `coeff'

areg R`var' $indcontrols $agecontrols $offcontrols ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr  , absorb(ppn) vce(cluster ppn)
regsave using `coeff', addvar(Atyear_2000,0,0) ci replace

preserve
use `coeff', clear
keep var coef ci_lower ci_upper
gen year=substr(var,8,4)
gen grp=substr(var,1,2)
destring year, replace force
keep if grp=="At"
keep if year>=1995 & year<=2008
replace ci_lower=0 if year==2000
replace ci_upper=0 if year==2000
drop var
sort year
gen time=year - 2001 

rename coef E`var'
rename ci_lower E`var'_l
rename ci_upper E`var'_u

cap drop fakeline
gen fakeline=.

*event study
twoway (line E`var' time, lc(black) lp(solid) lw(medthick)) ///
 (rarea E`var'_l E`var'_u time, color(gs12%50) lp(shortdash) lc(black) ) ///
 (scatter fakeline time, mc(red) msym(|)) ///
 , yline(0) xline(-1, lc(red)) scheme(s1mono) ///
 xlabel(-5(1)7) xtitle("Years (relative to onset)") xsc(titlegap(1)) ///
 ylabel(, angle(horizontal)) ytitle("Estimates") ysc(titlegap(0)) ///
 legend(off)  
 graph export "results/Figure4_ES_`var'.pdf", replace 

restore
}




********************************************************************************
********************************************************************************
*1. Figure 5 
********************************************************************************
********************************************************************************

*Raw data

use "data/panel.dta", clear

global depvars2 "off_robbery off_breakenter off_mv_theft off_theft_nonmv"

foreach var of varlist $depvars2 {

preserve 
keep if treated==0  & (pre_off_breakenter>0 | pre_off_mv_theft>0 | pre_off_robbery>0)
collapse (sum) `var' (count) obs=ppn, by(yq treated)
replace treated=3 
tempfile alt
save `alt'
restore

preserve 
collapse (sum) `var' (count) obs=ppn, by(yq treated)
append using `alt'

gen R`var'=1000*4*(`var'/obs)
keep R`var' yq treated
reshape wide R`var', i(yq) j(treated)
format yq %tq

cap drop fakeline
gen fakeline=.

drop if yq<=yq(1995,3)

twoway (line R`var'1 yq, lc(black) lp(solid) lw(thick)) (line R`var'0 yq, lc(gs4) lp(shortdash) lw(thick)) (line R`var'3 yq, lc(gs8) lp(longdash) lw(thick)) ///
 (scatter fakeline yq, mc(red) msymbol(|) msize(large) ) ///
,  xline(`=yq(2000,4)', lc(red)  lw(thick)   ) scheme(s1mono) ///
ytitle("Rate per 1000") ylabel(, angle(horizontal) format(%9.0fc)) ///
xtitle("")  xlabel(`=yq(1996,4)'(8)`=yq(2008,1)') ///
 xtick(`=yq(1996,4)'(4)`=yq(2008,1)') ///
  legend(off) 
graph export "results/Figure5_RAW_`var'.pdf", replace 

restore
}

*Event Study

global indcontrols "treated i.female i.indig i.female_missing i.indig_missing  "
global agecontrols "i.age_int  i.age_int#i.female i.age_int#i.indig "
global offcontrols "i.first_drug_off_ym"

*plots with alternative propert offending control group 

foreach var of varlist  off_robbery off_breakenter off_mv_theft off_theft_nonmv {
	
cap drop Ayear_2000 Atyear_2000

capture drop R`var' 
gen R`var'=`var'*1000*4

tempfile coeff
save `coeff'

areg R`var' $indcontrols $agecontrols $offcontrols  ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr ///
 if (treated==1 | (treated==0  & (pre_off_breakenter>0 | pre_off_mv_theft>0 | pre_off_robbery>0))), absorb(ppn) vce(cluster ppn)
regsave using `coeff' , addvar(Atyear_2000,0,0) ci replace

preserve

use `coeff', clear
keep var coef ci_lower ci_upper
gen year=substr(var,8,4)
gen grp=substr(var,1,2)
destring year, replace force
keep if grp=="At"
keep if year>=1995 & year<=2008
replace ci_lower=0 if year==2000
replace ci_upper=0 if year==2000
drop var
sort year
gen time=year - 2001 

rename coef E`var'
rename ci_lower E`var'_l
rename ci_upper E`var'_u

cap drop fakeline
gen fakeline=.
gen alt=1

 *figures with just alternative control group
twoway (line E`var' time if alt==1, lc(black) lp(solid) lw(medthick)) ///
 (rarea E`var'_l E`var'_u time if alt==1, color(gs12%50) lp(shortdash) lc(black) ) ///
 (scatter fakeline time, mc(red) msym(|)) ///
 , yline(0) xline(-1, lc(red)) scheme(s1mono) ///
 xlabel(-5(1)7) xtitle("Years (relative to onset)") xsc(titlegap(1)) ///
 ylabel(, angle(horizontal)) ytitle("Estimates") ysc(titlegap(0)) ///
 legend(off)  
 graph export "results/Figure5_ES_`var'.pdf", replace 
 
restore
} 
 

 
 
 

/*******************************************************************************
********************************************************************************
Table 1
********************************************************************************
*******************************************************************************/ 

capture ssc install texdoc 

use "data/panel.dta", clear

gen off_drug_harduse=off_drug_hard_nonO_use

global depvarsnofe "mortality mortality_opiates2 mortality_nonopioid"
global depvarsfe1 " off_drug_harduse  off_drug_cocaine_use off_drug_amph_use  off_violent_nonrobb off_murder_plus off_assault_serious off_assault_common  off_sexassault off_harrassment   "
global depvarsfe2 "off_property_serious off_robbery off_breakenter off_mv_theft off_theft_nonmv"

global indcontrols "treated i.female i.indig i.female_missing i.indig_missing  "
global agecontrols "i.age_int  i.age_int#i.female i.age_int#i.indig "
global offcontrols "i.first_drug_off_ym"

global coeftypes "pre post1 post2 post3"

*** Table - no ind. FE
foreach var of varlist $depvarsnofe  {
capture drop R*
gen R`var'=`var'*1000*4

* Baseline - opioid outcomes

reg R`var' $indcontrols $agecontrols $offcontrols Atyear_*  Ayear_* q4y2000 q4y2000_T i.qtr treated ,  vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
(posttotal: ((_b[Atyear_2001] + _b[Atyear_2002] + _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/8)) ///
, post 

foreach c of global coeftypes {
global b_`var'_`c': di %6.2fc _b[`c']
global se_`var'_`c': di %6.2fc _se[`c']
test `c'=0
global `var'_`c'_p: di %12.2fc r(p)
*global `var'_`c'_star=cond(${`var'_`c'_p}<.01,"***",cond(${`var'_`c'_p}<.05,"**",cond(${`var'_`c'_p}<.10,"*","")))
global `var'_`c'_star=cond(${`var'_`c'_p}<.01,"**",cond(${`var'_`c'_p}<.05,"*",""))
}
sum R`var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1 & e(sample)
global ymean_`var': di %6.2fc r(mean)
}

*with individual FE
foreach var of varlist $depvarsfe1  {
capture drop R*
gen R`var'=`var'*1000*4

* Baseline - opioid outcomes

areg R`var' $indcontrols $agecontrols $offcontrols Atyear_*  Ayear_* q4y2000 q4y2000_T i.qtr , absorb(ppn)  vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
(posttotal: ((_b[Atyear_2001] + _b[Atyear_2002] + _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/8)) ///
, post 

foreach c of global coeftypes {
global b_`var'_`c': di %6.2fc _b[`c']
global se_`var'_`c': di %6.2fc _se[`c']
test `c'=0
global `var'_`c'_p: di %12.2fc r(p)
global `var'_`c'_star=cond(${`var'_`c'_p}<.01,"**",cond(${`var'_`c'_p}<.05,"*",""))
}
sum R`var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1 & e(sample)
global ymean_`var': di %6.2fc r(mean)
}

*with individual FE - alternative control group for property 
foreach var of varlist $depvarsfe2  {
capture drop R*
gen R`var'=`var'*1000*4

* Baseline - opioid outcomes

areg R`var' $indcontrols $agecontrols $offcontrols Atyear_*  Ayear_* q4y2000 q4y2000_T i.qtr ///
if (treated==1 | (treated==0  & (pre_off_breakenter>0 | pre_off_mv_theft>0 | pre_off_robbery>0))) ///
, absorb(ppn)  vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
(posttotal: ((_b[Atyear_2001] + _b[Atyear_2002] + _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/8)) ///
, post 

foreach c of global coeftypes {
global b_`var'_`c': di %6.2fc _b[`c']
global se_`var'_`c': di %6.2fc _se[`c']
test `c'=0
global `var'_`c'_p: di %12.2fc r(p)
global `var'_`c'_star=cond(${`var'_`c'_p}<.01,"**",cond(${`var'_`c'_p}<.05,"*",""))
}
sum R`var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1 & e(sample)
global ymean_`var': di %6.2fc r(mean)
}

texdoc init "results/Table1.tex", replace force
tex & & \multicolumn{8}{c}{\specialcell{Coefficients (standard errors)}} \\
tex \cmidrule(lr){3-10} \multicolumn{1}{l}{\specialcell{\; \\ \; \\ \; \\ Outcome}} &  \multicolumn{1}{c}{\specialcell{Avg. for \\ treatment \\ group \\ year -1}} & \multicolumn{2}{c}{\specialcell{\; \\ \; \\ Years \\ -5 to -2}} &  \multicolumn{2}{c}{\specialcell{\; \\ \; \\ Year \\ 0}} & \multicolumn{2}{c}{\specialcell{\; \\ \; \\ Year \\ 1}} &  \multicolumn{2}{c}{\specialcell{\; \\ \; \\ Years \\ 2 to 7}}   \\ \hline
tex \\
tex & & \multicolumn{8}{c}{\specialcell{A. Mortality}} \\
tex \\
tex Opioid-related  & \multicolumn{1}{c}{${ymean_mortality_opiates2}} &  \multicolumn{1}{c}{ ${b_mortality_opiates2_pre}${mortality_opiates2_pre_star} } &  \multicolumn{1}{c}{(${se_mortality_opiates2_pre})} &  \multicolumn{1}{c}{ ${b_mortality_opiates2_post1}${mortality_opiates2_post1_star} } &  \multicolumn{1}{c}{(${se_mortality_opiates2_post1})} &  \multicolumn{1}{c}{ ${b_mortality_opiates2_post2}${mortality_opiates2_post2_star} } &  \multicolumn{1}{c}{(${se_mortality_opiates2_post2})} &  \multicolumn{1}{c}{ ${b_mortality_opiates2_post3}${mortality_opiates2_post3_star} } &  \multicolumn{1}{c}{(${se_mortality_opiates2_post3})}   \\
tex Non-opioid & \multicolumn{1}{c}{${ymean_mortality_nonopioid}} &  \multicolumn{1}{c}{ ${b_mortality_nonopioid_pre}${mortality_nonopioid_pre_star} } &  \multicolumn{1}{c}{(${se_mortality_nonopioid_pre})} &  \multicolumn{1}{c}{ ${b_mortality_nonopioid_post1}${mortality_nonopioid_post1_star} } &  \multicolumn{1}{c}{(${se_mortality_nonopioid_post1})} &  \multicolumn{1}{c}{ ${b_mortality_nonopioid_post2}${mortality_nonopioid_post2_star} } &  \multicolumn{1}{c}{(${se_mortality_nonopioid_post2})} &  \multicolumn{1}{c}{ ${b_mortality_nonopioid_post3}${mortality_nonopioid_post3_star} } &  \multicolumn{1}{c}{(${se_mortality_nonopioid_post3})}   \\
tex Any cause & \multicolumn{1}{c}{${ymean_mortality}} &  \multicolumn{1}{c}{ ${b_mortality_pre}${mortality_pre_star} } &  \multicolumn{1}{c}{(${se_mortality_pre})} &  \multicolumn{1}{c}{ ${b_mortality_post1}${mortality_post1_star} } &  \multicolumn{1}{c}{(${se_mortality_post1})} &  \multicolumn{1}{c}{ ${b_mortality_post2}${mortality_post2_star} } &  \multicolumn{1}{c}{(${se_mortality_post2})} &  \multicolumn{1}{c}{ ${b_mortality_post3}${mortality_post3_star} } &  \multicolumn{1}{c}{(${se_mortality_post3})}   \\
tex \\
tex & & \multicolumn{8}{c}{\specialcell{B. Non-opioid drug use/possession}} \\
tex \\
tex \specialcell{All hard drugs \;\;\;\;  \\ (excl. opioids)} & \multicolumn{1}{c}{${ymean_off_drug_harduse}} &  \multicolumn{1}{c}{ ${b_off_drug_harduse_pre}${off_drug_harduse_pre_star} } &  \multicolumn{1}{c}{(${se_off_drug_harduse_pre})} &  \multicolumn{1}{c}{ ${b_off_drug_harduse_post1}${off_drug_harduse_post1_star} } &  \multicolumn{1}{c}{(${se_off_drug_harduse_post1})} &  \multicolumn{1}{c}{ ${b_off_drug_harduse_post2}${off_drug_harduse_post2_star} } &  \multicolumn{1}{c}{(${se_off_drug_harduse_post2})} &  \multicolumn{1}{c}{ ${b_off_drug_harduse_post3}${off_drug_harduse_post3_star} } &  \multicolumn{1}{c}{(${se_off_drug_harduse_post3})}   \\
tex Cocaine  & \multicolumn{1}{c}{${ymean_off_drug_cocaine_use}} &  \multicolumn{1}{c}{ ${b_off_drug_cocaine_use_pre}${off_drug_cocaine_use_pre_star} } &  \multicolumn{1}{c}{(${se_off_drug_cocaine_use_pre})} &  \multicolumn{1}{c}{ ${b_off_drug_cocaine_use_post1}${off_drug_cocaine_use_post1_star} } &  \multicolumn{1}{c}{(${se_off_drug_cocaine_use_post1})} &  \multicolumn{1}{c}{ ${b_off_drug_cocaine_use_post2}${off_drug_cocaine_use_post2_star} } &  \multicolumn{1}{c}{(${se_off_drug_cocaine_use_post2})} &  \multicolumn{1}{c}{ ${b_off_drug_cocaine_use_post3}${off_drug_cocaine_use_post3_star} } &  \multicolumn{1}{c}{(${se_off_drug_cocaine_use_post3})}   \\
tex Methamphetamine & \multicolumn{1}{c}{${ymean_off_drug_amph_use}} &  \multicolumn{1}{c}{ ${b_off_drug_amph_use_pre}${off_drug_amph_use_pre_star} } &  \multicolumn{1}{c}{(${se_off_drug_amph_use_pre})} &  \multicolumn{1}{c}{ ${b_off_drug_amph_use_post1}${off_drug_amph_use_post1_star} } &  \multicolumn{1}{c}{(${se_off_drug_amph_use_post1})} &  \multicolumn{1}{c}{ ${b_off_drug_amph_use_post2}${off_drug_amph_use_post2_star} } &  \multicolumn{1}{c}{(${se_off_drug_amph_use_post2})} &  \multicolumn{1}{c}{ ${b_off_drug_amph_use_post3}${off_drug_amph_use_post3_star} } &  \multicolumn{1}{c}{(${se_off_drug_amph_use_post3})}   \\
tex \\
tex & & \multicolumn{8}{c}{\specialcell{C. Violent offenses}} \\
tex \\
tex \specialcell{Violent \;\;\;\;\;\;\;\;\;\;\;\; \\ (excl. robbery)} & \multicolumn{1}{c}{${ymean_off_violent_nonrobb}} &  \multicolumn{1}{c}{ ${b_off_violent_nonrobb_pre}${off_violent_nonrobb_pre_star} } &  \multicolumn{1}{c}{(${se_off_violent_nonrobb_pre})} &  \multicolumn{1}{c}{ ${b_off_violent_nonrobb_post1}${off_violent_nonrobb_post1_star} } &  \multicolumn{1}{c}{(${se_off_violent_nonrobb_post1})} &  \multicolumn{1}{c}{ ${b_off_violent_nonrobb_post2}${off_violent_nonrobb_post2_star} } &  \multicolumn{1}{c}{(${se_off_violent_nonrobb_post2})} &  \multicolumn{1}{c}{ ${b_off_violent_nonrobb_post3}${off_violent_nonrobb_post3_star} } &  \multicolumn{1}{c}{(${se_off_violent_nonrobb_post3})}   \\
tex \specialcell{Homicide \& \;\;\; \\ manslaughter} & \multicolumn{1}{c}{${ymean_off_murder_plus}} &  \multicolumn{1}{c}{ ${b_off_murder_plus_pre}${off_murder_plus_pre_star} } &  \multicolumn{1}{c}{(${se_off_murder_plus_pre})} &  \multicolumn{1}{c}{ ${b_off_murder_plus_post1}${off_murder_plus_post1_star} } &  \multicolumn{1}{c}{(${se_off_murder_plus_post1})} &  \multicolumn{1}{c}{ ${b_off_murder_plus_post2}${off_murder_plus_post2_star} } &  \multicolumn{1}{c}{(${se_off_murder_plus_post2})} &  \multicolumn{1}{c}{ ${b_off_murder_plus_post3}${off_murder_plus_post3_star} } &  \multicolumn{1}{c}{(${se_off_murder_plus_post3})}   \\
tex Serious assault & \multicolumn{1}{c}{${ymean_off_assault_serious}} &  \multicolumn{1}{c}{ ${b_off_assault_serious_pre}${off_assault_serious_pre_star} } &  \multicolumn{1}{c}{(${se_off_assault_serious_pre})} &  \multicolumn{1}{c}{ ${b_off_assault_serious_post1}${off_assault_serious_post1_star} } &  \multicolumn{1}{c}{(${se_off_assault_serious_post1})} &  \multicolumn{1}{c}{ ${b_off_assault_serious_post2}${off_assault_serious_post2_star} } &  \multicolumn{1}{c}{(${se_off_assault_serious_post2})} &  \multicolumn{1}{c}{ ${b_off_assault_serious_post3}${off_assault_serious_post3_star} } &  \multicolumn{1}{c}{(${se_off_assault_serious_post3})}   \\
tex Common assault & \multicolumn{1}{c}{${ymean_off_assault_common}} &  \multicolumn{1}{c}{ ${b_off_assault_common_pre}${off_assault_common_pre_star} } &  \multicolumn{1}{c}{(${se_off_assault_common_pre})} &  \multicolumn{1}{c}{ ${b_off_assault_common_post1}${off_assault_common_post1_star} } &  \multicolumn{1}{c}{(${se_off_assault_common_post1})} &  \multicolumn{1}{c}{ ${b_off_assault_common_post2}${off_assault_common_post2_star} } &  \multicolumn{1}{c}{(${se_off_assault_common_post2})} &  \multicolumn{1}{c}{ ${b_off_assault_common_post3}${off_assault_common_post3_star} } &  \multicolumn{1}{c}{(${se_off_assault_common_post3})}   \\
tex Sexual assault & \multicolumn{1}{c}{${ymean_off_sexassault}} &  \multicolumn{1}{c}{ ${b_off_sexassault_pre}${off_sexassault_pre_star} } &  \multicolumn{1}{c}{(${se_off_sexassault_pre})} &  \multicolumn{1}{c}{ ${b_off_sexassault_post1}${off_sexassault_post1_star} } &  \multicolumn{1}{c}{(${se_off_sexassault_post1})} &  \multicolumn{1}{c}{ ${b_off_sexassault_post2}${off_sexassault_post2_star} } &  \multicolumn{1}{c}{(${se_off_sexassault_post2})} &  \multicolumn{1}{c}{ ${b_off_sexassault_post3}${off_sexassault_post3_star} } &  \multicolumn{1}{c}{(${se_off_sexassault_post3})}   \\
tex Harrassment & \multicolumn{1}{c}{${ymean_off_harrassment}} &  \multicolumn{1}{c}{ ${b_off_harrassment_pre}${off_harrassment_pre_star} } &  \multicolumn{1}{c}{(${se_off_harrassment_pre})} &  \multicolumn{1}{c}{ ${b_off_harrassment_post1}${off_harrassment_post1_star} } &  \multicolumn{1}{c}{(${se_off_harrassment_post1})} &  \multicolumn{1}{c}{ ${b_off_harrassment_post2}${off_harrassment_post2_star} } &  \multicolumn{1}{c}{(${se_off_harrassment_post2})} &  \multicolumn{1}{c}{ ${b_off_harrassment_post3}${off_harrassment_post3_star} } &  \multicolumn{1}{c}{(${se_off_harrassment_post3})}   \\
tex \\
tex & & \multicolumn{8}{c}{\specialcell{D. Property offenses (\textit{with alterntative control group}) }} \\
tex \\
tex \specialcell{Serious property offenses \\ (incl. robbery)} & \multicolumn{1}{c}{${ymean_off_property_serious}} &  \multicolumn{1}{c}{ ${b_off_property_serious_pre}${off_property_serious_pre_star} } &  \multicolumn{1}{c}{(${se_off_property_serious_pre})} &  \multicolumn{1}{c}{ ${b_off_property_serious_post1}${off_property_serious_post1_star} } &  \multicolumn{1}{c}{(${se_off_property_serious_post1})} &  \multicolumn{1}{c}{ ${b_off_property_serious_post2}${off_property_serious_post2_star} } &  \multicolumn{1}{c}{(${se_off_property_serious_post2})} &  \multicolumn{1}{c}{ ${b_off_property_serious_post3}${off_property_serious_post3_star} } &  \multicolumn{1}{c}{(${se_off_property_serious_post3})}   \\
tex Robbery & \multicolumn{1}{c}{${ymean_off_robbery}} &  \multicolumn{1}{c}{ ${b_off_robbery_pre}${off_robbery_pre_star} } &  \multicolumn{1}{c}{(${se_off_robbery_pre})} &  \multicolumn{1}{c}{ ${b_off_robbery_post1}${off_robbery_post1_star} } &  \multicolumn{1}{c}{(${se_off_robbery_post1})} &  \multicolumn{1}{c}{ ${b_off_robbery_post2}${off_robbery_post2_star} } &  \multicolumn{1}{c}{(${se_off_robbery_post2})} &  \multicolumn{1}{c}{ ${b_off_robbery_post3}${off_robbery_post3_star} } &  \multicolumn{1}{c}{(${se_off_robbery_post3})}   \\
tex Burglary  & \multicolumn{1}{c}{${ymean_off_breakenter}} &  \multicolumn{1}{c}{ ${b_off_breakenter_pre}${off_breakenter_pre_star} } &  \multicolumn{1}{c}{(${se_off_breakenter_pre})} &  \multicolumn{1}{c}{ ${b_off_breakenter_post1}${off_breakenter_post1_star} } &  \multicolumn{1}{c}{(${se_off_breakenter_post1})} &  \multicolumn{1}{c}{ ${b_off_breakenter_post2}${off_breakenter_post2_star} } &  \multicolumn{1}{c}{(${se_off_breakenter_post2})} &  \multicolumn{1}{c}{ ${b_off_breakenter_post3}${off_breakenter_post3_star} } &  \multicolumn{1}{c}{(${se_off_breakenter_post3})}   \\
tex Vehicle theft & \multicolumn{1}{c}{${ymean_off_mv_theft}} &  \multicolumn{1}{c}{ ${b_off_mv_theft_pre}${off_mv_theft_pre_star} } &  \multicolumn{1}{c}{(${se_off_mv_theft_pre})} &  \multicolumn{1}{c}{ ${b_off_mv_theft_post1}${off_mv_theft_post1_star} } &  \multicolumn{1}{c}{(${se_off_mv_theft_post1})} &  \multicolumn{1}{c}{ ${b_off_mv_theft_post2}${off_mv_theft_post2_star} } &  \multicolumn{1}{c}{(${se_off_mv_theft_post2})} &  \multicolumn{1}{c}{ ${b_off_mv_theft_post3}${off_mv_theft_post3_star} } &  \multicolumn{1}{c}{(${se_off_mv_theft_post3})}   \\
tex \\
tex Petty/other theft  & \multicolumn{1}{c}{${ymean_off_theft_nonmv}} &  \multicolumn{1}{c}{ ${b_off_theft_nonmv_pre}${off_theft_nonmv_pre_star} } &  \multicolumn{1}{c}{(${se_off_theft_nonmv_pre})} &  \multicolumn{1}{c}{ ${b_off_theft_nonmv_post1}${off_theft_nonmv_post1_star} } &  \multicolumn{1}{c}{(${se_off_theft_nonmv_post1})} &  \multicolumn{1}{c}{ ${b_off_theft_nonmv_post2}${off_theft_nonmv_post2_star} } &  \multicolumn{1}{c}{(${se_off_theft_nonmv_post2})} &  \multicolumn{1}{c}{ ${b_off_theft_nonmv_post3}${off_theft_nonmv_post3_star} } &  \multicolumn{1}{c}{(${se_off_theft_nonmv_post3})}   \\
texdoc close

 
 
**************************************
** Table 2 
**************************************


use "data/panel.dta", clear

*shorten variable names for globals
gen off_drug_harduse=off_drug_hard_nonO_use
gen off_drug_cuse=off_drug_cocaine_use
gen off_drug_ause=off_drug_amph_use
gen off_violnonrobb=off_violent_nonrobb
gen off_proprobb=off_property_robb
gen off_sassault=off_assault_serious
gen off_cassault=off_assault_common
gen off_sprop=off_property_serious

global depvarsnofe "mortality mortality_opiates2"
global depvarsfe1 " off_drug_harduse  off_violnonrobb "
global depvarsfe2 " off_sprop "

global indcontrols "treated i.female i.indig i.female_missing i.indig_missing  "
global agecontrols "i.age_int  i.age_int#i.female i.age_int#i.indig "
global offcontrols "i.first_drug_off_ym"

global coeftypes "post1 post3"

gen het1=(female==1) //female
gen het2=(female==0) //male

*age at onset  of supply shock 
sort ppn yq
bys ppn: gen byrtemp=round(year(dofq(yq))-age) if _n==1
bys ppn: egen birthyr=max(byrtemp)
gen het3=(birthyr>=1970) //young
gen het4=(birthyr<1970) //old




forvalues j=1/4 {

preserve 

	global At "Atyear_1995 Atyear_1996 Atyear_1997 Atyear_1998 Atyear_1999  Atyear_2001 Atyear_2002 Atyear_2003 Atyear_2004 Atyear_2005 Atyear_2006 Atyear_2007 Atyear_2008"
	
	foreach var of varlist $At {
	gen `var'_O=0
	replace `var'_O=`var' if het`j'==0 & treated==1
	replace `var'=`var' if het`j'==1 & treated==1
	replace `var'=0 if (het`j'==0 | treated==0)
}


	
*** Table - no ind. FE*
foreach var of varlist $depvarsnofe  {

capture drop R*
gen R`var'=`var'*1000*4

reg R`var' $indcontrols $agecontrols $offcontrols Atyear_*  Ayear_* q4y2000 q4y2000_T i.qtr treated,  vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
(pt: ((_b[Atyear_2001] + _b[Atyear_2002] + _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/8)) ///
(pre_O: (( _b[Atyear_1996_O]+ _b[Atyear_1997_O]+_b[Atyear_1998_O]+_b[Atyear_1999_O])/4)) ///
(post1_O: (_b[Atyear_2001_O])) /// 
(post2_O:  (_b[Atyear_2002_O])) ///
(post3_O: (( _b[Atyear_2003_O] + _b[Atyear_2004_O]+ _b[Atyear_2005_O] + _b[Atyear_2006_O] + _b[Atyear_2007_O] + _b[Atyear_2008_O])/6)) ///
(pt_O: ((_b[Atyear_2001_O] + _b[Atyear_2002_O] + _b[Atyear_2003_O] + _b[Atyear_2004_O]+ _b[Atyear_2005_O] + _b[Atyear_2006_O] + _b[Atyear_2007_O] + _b[Atyear_2008_O])/8)) ///
, post 


foreach c of global coeftypes {
global b_`var'_`c'_`j': di %6.2fc _b[`c']
global se_`var'_`c'_`j': di %6.2fc _se[`c']
test `c'=0
global `var'_`c'_`j'_p: di %12.2fc r(p)
*global `var'_`c'_`j'_star=cond(${`var'_`c'_`j'_p}<.01,"***",cond(${`var'_`c'_`j'_p}<.05,"**",cond(${`var'_`c'_`j'_p}<.10,"*","")))
global `var'_`c'_`j'_star=cond(${`var'_`c'_`j'_p}<.01,"**",cond(${`var'_`c'_`j'_p}<.05,"*",""))

test `c'=`c'_O
global `var'_`c'_`j'_pO: di %12.2fc r(p)
}
sum R`var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1 & e(sample) & het`j'==1
global ymean_`var'_`j': di %6.2fc r(mean)


}


*with individual FE
foreach var of varlist $depvarsfe1  {
capture drop R*
gen R`var'=`var'*1000*4

* Baseline - opioid outcomes

areg R`var' $indcontrols $agecontrols $offcontrols Atyear_*  Ayear_* q4y2000 q4y2000_T i.qtr  , absorb(ppn)  vce(cluster ppn)

local rsquare=e(r2)


nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
(pt: ((_b[Atyear_2001] + _b[Atyear_2002] + _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/8)) ///
(pre_O: (( _b[Atyear_1996_O]+ _b[Atyear_1997_O]+_b[Atyear_1998_O]+_b[Atyear_1999_O])/4)) ///
(post1_O: (_b[Atyear_2001_O])) /// 
(post2_O:  (_b[Atyear_2002_O])) ///
(post3_O: (( _b[Atyear_2003_O] + _b[Atyear_2004_O]+ _b[Atyear_2005_O] + _b[Atyear_2006_O] + _b[Atyear_2007_O] + _b[Atyear_2008_O])/6)) ///
(pt_O: ((_b[Atyear_2001_O] + _b[Atyear_2002_O] + _b[Atyear_2003_O] + _b[Atyear_2004_O]+ _b[Atyear_2005_O] + _b[Atyear_2006_O] + _b[Atyear_2007_O] + _b[Atyear_2008_O])/8)) ///
, post 


foreach c of global coeftypes {
global b_`var'_`c'_`j': di %6.2fc _b[`c']
global se_`var'_`c'_`j': di %6.2fc _se[`c']
test `c'=0
global `var'_`c'_`j'_p: di %12.2fc r(p)
*global `var'_`c'_`j'_star=cond(${`var'_`c'_`j'_p}<.01,"***",cond(${`var'_`c'_`j'_p}<.05,"**",cond(${`var'_`c'_`j'_p}<.10,"*","")))
global `var'_`c'_`j'_star=cond(${`var'_`c'_`j'_p}<.01,"**",cond(${`var'_`c'_`j'_p}<.05,"*",""))

test `c'=`c'_O
global `var'_`c'_`j'_pO: di %12.2fc r(p)
}

sum R`var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1 & e(sample) & het`j'==1
global ymean_`var'_`j': di %6.2fc r(mean)
}



*with individual FE - alternative control group for property 
foreach var of varlist $depvarsfe2  {
capture drop R*
gen R`var'=`var'*1000*4

* Baseline - opioid outcomes

areg R`var' $indcontrols $agecontrols $offcontrols Atyear_*  Ayear_* q4y2000 q4y2000_T i.qtr ///
if (treated==1 | (treated==0  & (pre_off_breakenter>0 | pre_off_mv_theft>0 | pre_off_robbery>0)))  ///
, absorb(ppn)  vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
(pt: ((_b[Atyear_2001] + _b[Atyear_2002] + _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/8)) ///
(pre_O: (( _b[Atyear_1996_O]+ _b[Atyear_1997_O]+_b[Atyear_1998_O]+_b[Atyear_1999_O])/4)) ///
(post1_O: (_b[Atyear_2001_O])) /// 
(post2_O:  (_b[Atyear_2002_O])) ///
(post3_O: (( _b[Atyear_2003_O] + _b[Atyear_2004_O]+ _b[Atyear_2005_O] + _b[Atyear_2006_O] + _b[Atyear_2007_O] + _b[Atyear_2008_O])/6)) ///
(pt_O: ((_b[Atyear_2001_O] + _b[Atyear_2002_O] + _b[Atyear_2003_O] + _b[Atyear_2004_O]+ _b[Atyear_2005_O] + _b[Atyear_2006_O] + _b[Atyear_2007_O] + _b[Atyear_2008_O])/8)) ///
, post 


foreach c of global coeftypes {
global b_`var'_`c'_`j': di %6.2fc _b[`c']
global se_`var'_`c'_`j': di %6.2fc _se[`c']
test `c'=0
global `var'_`c'_`j'_p: di %12.2fc r(p)
*global `var'_`c'_`j'_star=cond(${`var'_`c'_`j'_p}<.01,"***",cond(${`var'_`c'_`j'_p}<.05,"**",cond(${`var'_`c'_`j'_p}<.10,"*","")))
global `var'_`c'_`j'_star=cond(${`var'_`c'_`j'_p}<.01,"**",cond(${`var'_`c'_`j'_p}<.05,"*",""))

test `c'=`c'_O
global `var'_`c'_`j'_pO: di %12.2fc r(p)
}

sum R`var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1 & e(sample) & het`j'==1
global ymean_`var'_`j': di %6.2fc r(mean)
}

restore
}


*split by age at entry 
capture drop het13 het14
preserve
sort ppn yq
bys ppn: keep if _n==1
sum age_at_entry if treated==1, detail
local median=r(p50)
gen het13=(age_at_entry<=`median')
gen het14=(age_at_entry>`median')
tab het13
tab het14
keep het13 het14 ppn
tempfile hetp
save `hetp'
restore
merge m:1 ppn using `hetp'
drop _merge

*split by year at entry 
preserve
sort ppn yq
bys ppn: keep if _n==1
sum entry_year if treated==1, detail //1998
restore 

gen het15=(entry_year<1998)
gen het16=(entry_year>=1998)


forvalues j=13/16 {

preserve 

	global At "Atyear_1995 Atyear_1996 Atyear_1997 Atyear_1998 Atyear_1999  Atyear_2001 Atyear_2002 Atyear_2003 Atyear_2004 Atyear_2005 Atyear_2006 Atyear_2007 Atyear_2008"
	
	foreach var of varlist $At {
	gen `var'_O=0
	replace `var'_O=`var' if het`j'==0 & treated==1
	replace `var'=`var' if het`j'==1 & treated==1
	replace `var'=0 if (het`j'==0 | treated==0)
}


	
*** Table - no ind. FE*
foreach var of varlist $depvarsnofe  {

capture drop R*
gen R`var'=`var'*1000*4

reg R`var' $indcontrols $agecontrols $offcontrols Atyear_*  Ayear_* q4y2000 q4y2000_T i.qtr treated,  vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
(pt: ((_b[Atyear_2001] + _b[Atyear_2002] + _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/8)) ///
(pre_O: (( _b[Atyear_1996_O]+ _b[Atyear_1997_O]+_b[Atyear_1998_O]+_b[Atyear_1999_O])/4)) ///
(post1_O: (_b[Atyear_2001_O])) /// 
(post2_O:  (_b[Atyear_2002_O])) ///
(post3_O: (( _b[Atyear_2003_O] + _b[Atyear_2004_O]+ _b[Atyear_2005_O] + _b[Atyear_2006_O] + _b[Atyear_2007_O] + _b[Atyear_2008_O])/6)) ///
(pt_O: ((_b[Atyear_2001_O] + _b[Atyear_2002_O] + _b[Atyear_2003_O] + _b[Atyear_2004_O]+ _b[Atyear_2005_O] + _b[Atyear_2006_O] + _b[Atyear_2007_O] + _b[Atyear_2008_O])/8)) ///
, post 


foreach c of global coeftypes {
global b_`var'_`c'_`j': di %6.2fc _b[`c']
global se_`var'_`c'_`j': di %6.2fc _se[`c']
test `c'=0
global `var'_`c'_`j'_p: di %12.2fc r(p)
*global `var'_`c'_`j'_star=cond(${`var'_`c'_`j'_p}<.01,"***",cond(${`var'_`c'_`j'_p}<.05,"**",cond(${`var'_`c'_`j'_p}<.10,"*","")))
global `var'_`c'_`j'_star=cond(${`var'_`c'_`j'_p}<.01,"**",cond(${`var'_`c'_`j'_p}<.05,"*",""))

test `c'=`c'_O
global `var'_`c'_`j'_pO: di %12.2fc r(p)
}
sum R`var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1 & e(sample) & het`j'==1
global ymean_`var'_`j': di %6.2fc r(mean)


}


*with individual FE
foreach var of varlist $depvarsfe1  {
capture drop R*
gen R`var'=`var'*1000*4

* Baseline - opioid outcomes

areg R`var' $indcontrols $agecontrols $offcontrols Atyear_*  Ayear_* q4y2000 q4y2000_T i.qtr  , absorb(ppn)  vce(cluster ppn)

local rsquare=e(r2)


nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
(pt: ((_b[Atyear_2001] + _b[Atyear_2002] + _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/8)) ///
(pre_O: (( _b[Atyear_1996_O]+ _b[Atyear_1997_O]+_b[Atyear_1998_O]+_b[Atyear_1999_O])/4)) ///
(post1_O: (_b[Atyear_2001_O])) /// 
(post2_O:  (_b[Atyear_2002_O])) ///
(post3_O: (( _b[Atyear_2003_O] + _b[Atyear_2004_O]+ _b[Atyear_2005_O] + _b[Atyear_2006_O] + _b[Atyear_2007_O] + _b[Atyear_2008_O])/6)) ///
(pt_O: ((_b[Atyear_2001_O] + _b[Atyear_2002_O] + _b[Atyear_2003_O] + _b[Atyear_2004_O]+ _b[Atyear_2005_O] + _b[Atyear_2006_O] + _b[Atyear_2007_O] + _b[Atyear_2008_O])/8)) ///
, post 


foreach c of global coeftypes {
global b_`var'_`c'_`j': di %6.2fc _b[`c']
global se_`var'_`c'_`j': di %6.2fc _se[`c']
test `c'=0
global `var'_`c'_`j'_p: di %12.2fc r(p)
*global `var'_`c'_`j'_star=cond(${`var'_`c'_`j'_p}<.01,"***",cond(${`var'_`c'_`j'_p}<.05,"**",cond(${`var'_`c'_`j'_p}<.10,"*","")))
global `var'_`c'_`j'_star=cond(${`var'_`c'_`j'_p}<.01,"**",cond(${`var'_`c'_`j'_p}<.05,"*",""))

test `c'=`c'_O
global `var'_`c'_`j'_pO: di %12.2fc r(p)
}

sum R`var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1 & e(sample) & het`j'==1
global ymean_`var'_`j': di %6.2fc r(mean)
}



*with individual FE - alternative control group for property 
foreach var of varlist $depvarsfe2  {
capture drop R*
gen R`var'=`var'*1000*4

* Baseline - opioid outcomes

areg R`var' $indcontrols $agecontrols $offcontrols Atyear_*  Ayear_* q4y2000 q4y2000_T i.qtr ///
if (treated==1 | (treated==0  & (pre_off_breakenter>0 | pre_off_mv_theft>0 | pre_off_robbery>0)))  ///
, absorb(ppn)  vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
(pt: ((_b[Atyear_2001] + _b[Atyear_2002] + _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/8)) ///
(pre_O: (( _b[Atyear_1996_O]+ _b[Atyear_1997_O]+_b[Atyear_1998_O]+_b[Atyear_1999_O])/4)) ///
(post1_O: (_b[Atyear_2001_O])) /// 
(post2_O:  (_b[Atyear_2002_O])) ///
(post3_O: (( _b[Atyear_2003_O] + _b[Atyear_2004_O]+ _b[Atyear_2005_O] + _b[Atyear_2006_O] + _b[Atyear_2007_O] + _b[Atyear_2008_O])/6)) ///
(pt_O: ((_b[Atyear_2001_O] + _b[Atyear_2002_O] + _b[Atyear_2003_O] + _b[Atyear_2004_O]+ _b[Atyear_2005_O] + _b[Atyear_2006_O] + _b[Atyear_2007_O] + _b[Atyear_2008_O])/8)) ///
, post 


foreach c of global coeftypes {
global b_`var'_`c'_`j': di %6.2fc _b[`c']
global se_`var'_`c'_`j': di %6.2fc _se[`c']
test `c'=0
global `var'_`c'_`j'_p: di %12.2fc r(p)
*global `var'_`c'_`j'_star=cond(${`var'_`c'_`j'_p}<.01,"***",cond(${`var'_`c'_`j'_p}<.05,"**",cond(${`var'_`c'_`j'_p}<.10,"*","")))
global `var'_`c'_`j'_star=cond(${`var'_`c'_`j'_p}<.01,"**",cond(${`var'_`c'_`j'_p}<.05,"*",""))

test `c'=`c'_O
global `var'_`c'_`j'_pO: di %12.2fc r(p)
}

sum R`var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1 & e(sample) & het`j'==1
global ymean_`var'_`j': di %6.2fc r(mean)
}

restore
}


texdoc init "results/Table2.tex", replace force
tex  &  \multicolumn{2}{c}{\specialcell{Gender \\ \; }} & \multicolumn{2}{c}{\specialcell{Age at \\ time of \\ supply shock }} &  \multicolumn{2}{c}{\specialcell{Year of first \\ opioid offense }} &  \multicolumn{2}{c}{\specialcell{Age at first \\ opioid offense}}  \\
tex \cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}\cmidrule(lr){8-9} & \multicolumn{1}{c}{\specialcell{Female \\ }} & \multicolumn{1}{c}{\specialcell{Male \\ }} & \multicolumn{1}{c}{\specialcell{$<30$ \\ }} & \multicolumn{1}{c}{\specialcell{$>=30$ \\ }}  &  \multicolumn{1}{c}{\specialcell{1995 \\ - 1997}} & \multicolumn{1}{c}{\specialcell{1998 \\ - 2000}} & \multicolumn{1}{c}{\specialcell{Below \\ med. \\ ($\le 26$)}} & \multicolumn{1}{c}{\specialcell{Above \\ med. \\ ($> 26$)}}  \\ \hline

tex \\
tex &  \multicolumn{8}{c}{\specialcell{A. Opioid-related mortality}} \\
tex \\
tex Short term (yr 0)  &  \multicolumn{1}{c}{ ${b_mortality_opiates2_post1_1}${mortality_opiates2_post1_1_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post1_2}${mortality_opiates2_post1_2_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post1_3}${mortality_opiates2_post1_3_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post1_4}${mortality_opiates2_post1_4_star} }  &  \multicolumn{1}{c}{ ${b_mortality_opiates2_post1_15}${mortality_opiates2_post1_15_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post1_16}${mortality_opiates2_post1_16_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post1_13}${mortality_opiates2_post1_13_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post1_14}${mortality_opiates2_post1_14_star} } \\
tex &  \multicolumn{1}{c}{ (${se_mortality_opiates2_post1_1}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post1_2}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post1_3}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post1_4}) } &  \multicolumn{1}{c}{ (${se_mortality_opiates2_post1_15}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post1_16}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post1_13}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post1_14}) }  \\
tex Longer term (yrs 2-7) &  \multicolumn{1}{c}{ ${b_mortality_opiates2_post3_1}${mortality_opiates2_post3_1_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post3_2}${mortality_opiates2_post3_2_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post3_3}${mortality_opiates2_post3_3_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post3_4}${mortality_opiates2_post3_4_star} }  &  \multicolumn{1}{c}{ ${b_mortality_opiates2_post3_15}${mortality_opiates2_post3_15_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post3_16}${mortality_opiates2_post3_16_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post3_13}${mortality_opiates2_post3_13_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post3_14}${mortality_opiates2_post3_14_star} }  \\
tex &  \multicolumn{1}{c}{ (${se_mortality_opiates2_post3_1}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post3_2}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post3_3}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post3_4}) }  &  \multicolumn{1}{c}{ (${se_mortality_opiates2_post3_15}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post3_16}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post3_13}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post3_14}) }   \vspace{0.25cm} \\ 
tex   Avg in yr -1  & \multicolumn{1}{c}{${ymean_mortality_opiates2_1}} & \multicolumn{1}{c}{${ymean_mortality_opiates2_2}} & \multicolumn{1}{c}{${ymean_mortality_opiates2_3}} & \multicolumn{1}{c}{${ymean_mortality_opiates2_4}} & \multicolumn{1}{c}{${ymean_mortality_opiates2_15}} & \multicolumn{1}{c}{${ymean_mortality_opiates2_16}} & \multicolumn{1}{c}{${ymean_mortality_opiates2_13}} & \multicolumn{1}{c}{${ymean_mortality_opiates2_14}}   \\

tex \\
tex &  \multicolumn{8}{c}{\specialcell{B. All-cause mortality}} \\
tex \\
tex Short term (yr 0)  &  \multicolumn{1}{c}{ ${b_mortality_post1_1}${mortality_post1_1_star} } & \multicolumn{1}{c}{ ${b_mortality_post1_2}${mortality_post1_2_star} } & \multicolumn{1}{c}{ ${b_mortality_post1_3}${mortality_post1_3_star} } & \multicolumn{1}{c}{ ${b_mortality_post1_4}${mortality_post1_4_star} }  &  \multicolumn{1}{c}{ ${b_mortality_post1_15}${mortality_post1_15_star} } & \multicolumn{1}{c}{ ${b_mortality_post1_16}${mortality_post1_16_star} } & \multicolumn{1}{c}{ ${b_mortality_post1_13}${mortality_post1_13_star} } & \multicolumn{1}{c}{ ${b_mortality_post1_14}${mortality_post1_14_star} } \\
tex &  \multicolumn{1}{c}{ (${se_mortality_post1_1}) } & \multicolumn{1}{c}{ (${se_mortality_post1_2}) } & \multicolumn{1}{c}{ (${se_mortality_post1_3}) } & \multicolumn{1}{c}{ (${se_mortality_post1_4}) } &  \multicolumn{1}{c}{ (${se_mortality_post1_15}) } & \multicolumn{1}{c}{ (${se_mortality_post1_16}) } & \multicolumn{1}{c}{ (${se_mortality_post1_13}) } & \multicolumn{1}{c}{ (${se_mortality_post1_14}) }  \\
tex Longer term (yrs 2-7) &  \multicolumn{1}{c}{ ${b_mortality_post3_1}${mortality_post3_1_star} } & \multicolumn{1}{c}{ ${b_mortality_post3_2}${mortality_post3_2_star} } & \multicolumn{1}{c}{ ${b_mortality_post3_3}${mortality_post3_3_star} } & \multicolumn{1}{c}{ ${b_mortality_post3_4}${mortality_post3_4_star} }  &  \multicolumn{1}{c}{ ${b_mortality_post3_15}${mortality_post3_15_star} } & \multicolumn{1}{c}{ ${b_mortality_post3_16}${mortality_post3_16_star} } & \multicolumn{1}{c}{ ${b_mortality_post3_13}${mortality_post3_13_star} } & \multicolumn{1}{c}{ ${b_mortality_post3_14}${mortality_post3_14_star} }  \\
tex &  \multicolumn{1}{c}{ (${se_mortality_post3_1}) } & \multicolumn{1}{c}{ (${se_mortality_post3_2}) } & \multicolumn{1}{c}{ (${se_mortality_post3_3}) } & \multicolumn{1}{c}{ (${se_mortality_post3_4}) }  &  \multicolumn{1}{c}{ (${se_mortality_post3_15}) } & \multicolumn{1}{c}{ (${se_mortality_post3_16}) } & \multicolumn{1}{c}{ (${se_mortality_post3_13}) } & \multicolumn{1}{c}{ (${se_mortality_post3_14}) }   \vspace{0.25cm} \\ 
tex   Avg in yr -1  & \multicolumn{1}{c}{${ymean_mortality_1}} & \multicolumn{1}{c}{${ymean_mortality_2}} & \multicolumn{1}{c}{${ymean_mortality_3}} & \multicolumn{1}{c}{${ymean_mortality_4}} & \multicolumn{1}{c}{${ymean_mortality_15}} & \multicolumn{1}{c}{${ymean_mortality_16}} & \multicolumn{1}{c}{${ymean_mortality_13}} & \multicolumn{1}{c}{${ymean_mortality_14}}   \\

tex \\
tex &  \multicolumn{8}{c}{\specialcell{C. Non-opioid drug use/possession}} \\
tex \\
tex Short term (yr 0)  &  \multicolumn{1}{c}{ ${b_off_drug_harduse_post1_1}${off_drug_harduse_post1_1_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post1_2}${off_drug_harduse_post1_2_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post1_3}${off_drug_harduse_post1_3_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post1_4}${off_drug_harduse_post1_4_star} }  &  \multicolumn{1}{c}{ ${b_off_drug_harduse_post1_15}${off_drug_harduse_post1_15_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post1_16}${off_drug_harduse_post1_16_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post1_13}${off_drug_harduse_post1_13_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post1_14}${off_drug_harduse_post1_14_star} } \\
tex &  \multicolumn{1}{c}{ (${se_off_drug_harduse_post1_1}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post1_2}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post1_3}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post1_4}) } &  \multicolumn{1}{c}{ (${se_off_drug_harduse_post1_15}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post1_16}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post1_13}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post1_14}) }  \\
tex Longer term (yrs 2-7) &  \multicolumn{1}{c}{ ${b_off_drug_harduse_post3_1}${off_drug_harduse_post3_1_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post3_2}${off_drug_harduse_post3_2_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post3_3}${off_drug_harduse_post3_3_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post3_4}${off_drug_harduse_post3_4_star} }  &  \multicolumn{1}{c}{ ${b_off_drug_harduse_post3_15}${off_drug_harduse_post3_15_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post3_16}${off_drug_harduse_post3_16_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post3_13}${off_drug_harduse_post3_13_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post3_14}${off_drug_harduse_post3_14_star} }  \\
tex &  \multicolumn{1}{c}{ (${se_off_drug_harduse_post3_1}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post3_2}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post3_3}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post3_4}) }  &  \multicolumn{1}{c}{ (${se_off_drug_harduse_post3_15}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post3_16}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post3_13}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post3_14}) }   \vspace{0.25cm} \\ 
tex   Avg in yr -1  & \multicolumn{1}{c}{${ymean_off_drug_harduse_1}} & \multicolumn{1}{c}{${ymean_off_drug_harduse_2}} & \multicolumn{1}{c}{${ymean_off_drug_harduse_3}} & \multicolumn{1}{c}{${ymean_off_drug_harduse_4}} & \multicolumn{1}{c}{${ymean_off_drug_harduse_15}} & \multicolumn{1}{c}{${ymean_off_drug_harduse_16}} & \multicolumn{1}{c}{${ymean_off_drug_harduse_13}} & \multicolumn{1}{c}{${ymean_off_drug_harduse_14}}   \\

tex \\
tex &  \multicolumn{8}{c}{\specialcell{D. Violent offenses (excl. robbery)}} \\
tex \\
tex Short term (yr 0)  &  \multicolumn{1}{c}{ ${b_off_violnonrobb_post1_1}${off_violnonrobb_post1_1_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post1_2}${off_violnonrobb_post1_2_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post1_3}${off_violnonrobb_post1_3_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post1_4}${off_violnonrobb_post1_4_star} }  &  \multicolumn{1}{c}{ ${b_off_violnonrobb_post1_15}${off_violnonrobb_post1_15_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post1_16}${off_violnonrobb_post1_16_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post1_13}${off_violnonrobb_post1_13_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post1_14}${off_violnonrobb_post1_14_star} } \\
tex &  \multicolumn{1}{c}{ (${se_off_violnonrobb_post1_1}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post1_2}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post1_3}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post1_4}) } &  \multicolumn{1}{c}{ (${se_off_violnonrobb_post1_15}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post1_16}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post1_13}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post1_14}) }  \\
tex Longer term (yrs 2-7) &  \multicolumn{1}{c}{ ${b_off_violnonrobb_post3_1}${off_violnonrobb_post3_1_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post3_2}${off_violnonrobb_post3_2_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post3_3}${off_violnonrobb_post3_3_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post3_4}${off_violnonrobb_post3_4_star} }  &  \multicolumn{1}{c}{ ${b_off_violnonrobb_post3_15}${off_violnonrobb_post3_15_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post3_16}${off_violnonrobb_post3_16_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post3_13}${off_violnonrobb_post3_13_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post3_14}${off_violnonrobb_post3_14_star} }  \\
tex &  \multicolumn{1}{c}{ (${se_off_violnonrobb_post3_1}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post3_2}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post3_3}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post3_4}) }  &  \multicolumn{1}{c}{ (${se_off_violnonrobb_post3_15}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post3_16}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post3_13}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post3_14}) }   \vspace{0.25cm} \\ 
tex   Avg in yr -1  & \multicolumn{1}{c}{${ymean_off_violnonrobb_1}} & \multicolumn{1}{c}{${ymean_off_violnonrobb_2}} & \multicolumn{1}{c}{${ymean_off_violnonrobb_3}} & \multicolumn{1}{c}{${ymean_off_violnonrobb_4}} & \multicolumn{1}{c}{${ymean_off_violnonrobb_15}} & \multicolumn{1}{c}{${ymean_off_violnonrobb_16}} & \multicolumn{1}{c}{${ymean_off_violnonrobb_13}} & \multicolumn{1}{c}{${ymean_off_violnonrobb_14}}   \\

tex \\
tex &  \multicolumn{8}{c}{\specialcell{E. Serious property offenses (robbery, burglary, vehicle theft)}} \\
tex \\
tex Short term (yr 0)  &  \multicolumn{1}{c}{ ${b_off_sprop_post1_1}${off_sprop_post1_1_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post1_2}${off_sprop_post1_2_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post1_3}${off_sprop_post1_3_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post1_4}${off_sprop_post1_4_star} }  &  \multicolumn{1}{c}{ ${b_off_sprop_post1_15}${off_sprop_post1_15_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post1_16}${off_sprop_post1_16_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post1_13}${off_sprop_post1_13_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post1_14}${off_sprop_post1_14_star} } \\
tex &  \multicolumn{1}{c}{ (${se_off_sprop_post1_1}) } & \multicolumn{1}{c}{ (${se_off_sprop_post1_2}) } & \multicolumn{1}{c}{ (${se_off_sprop_post1_3}) } & \multicolumn{1}{c}{ (${se_off_sprop_post1_4}) } &  \multicolumn{1}{c}{ (${se_off_sprop_post1_15}) } & \multicolumn{1}{c}{ (${se_off_sprop_post1_16}) } & \multicolumn{1}{c}{ (${se_off_sprop_post1_13}) } & \multicolumn{1}{c}{ (${se_off_sprop_post1_14}) }  \\
tex Longer term (yrs 2-7) &  \multicolumn{1}{c}{ ${b_off_sprop_post3_1}${off_sprop_post3_1_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post3_2}${off_sprop_post3_2_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post3_3}${off_sprop_post3_3_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post3_4}${off_sprop_post3_4_star} }  &  \multicolumn{1}{c}{ ${b_off_sprop_post3_15}${off_sprop_post3_15_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post3_16}${off_sprop_post3_16_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post3_13}${off_sprop_post3_13_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post3_14}${off_sprop_post3_14_star} }  \\
tex &  \multicolumn{1}{c}{ (${se_off_sprop_post3_1}) } & \multicolumn{1}{c}{ (${se_off_sprop_post3_2}) } & \multicolumn{1}{c}{ (${se_off_sprop_post3_3}) } & \multicolumn{1}{c}{ (${se_off_sprop_post3_4}) }  &  \multicolumn{1}{c}{ (${se_off_sprop_post3_15}) } & \multicolumn{1}{c}{ (${se_off_sprop_post3_16}) } & \multicolumn{1}{c}{ (${se_off_sprop_post3_13}) } & \multicolumn{1}{c}{ (${se_off_sprop_post3_14}) }   \vspace{0.25cm} \\ 
tex   Avg in yr -1  & \multicolumn{1}{c}{${ymean_off_sprop_1}} & \multicolumn{1}{c}{${ymean_off_sprop_2}} & \multicolumn{1}{c}{${ymean_off_sprop_3}} & \multicolumn{1}{c}{${ymean_off_sprop_4}} & \multicolumn{1}{c}{${ymean_off_sprop_15}} & \multicolumn{1}{c}{${ymean_off_sprop_16}} & \multicolumn{1}{c}{${ymean_off_sprop_13}} & \multicolumn{1}{c}{${ymean_off_sprop_14}}   \\

texdoc close




/*******************************************************************************
********************************************************************************

Figure A1: 
Prices of heroin, cocaine and methamphetamine, relative to year 2000

********************************************************************************
*******************************************************************************/ 
 

insheet using "data/price_heroin_cocaine_meth.csv", clear
gen constant=1

gen her_price1b=her_dose*inflate_all/her_pur_all*100
gen coc_price1b=coc_dose*inflate_all/coc_pur_all*100
gen meth_price1b=meth_dose*inflate_all/meth_pur_all*100

tempfile price
save `price'

insheet using "data/price_heroin_cocaine_meth.csv", clear
gen constant=1

keep if year==2000
drop year
gen her_price1a=her_dose*inflate_all/her_pur_all*100
gen coc_price1a=coc_dose*inflate_all/coc_pur_all*100
gen meth_price1a=meth_dose*inflate_all/meth_pur_all*100

merge m:m constant using `price'

*** making relative to 2000
gen her_price1 = her_price1b/her_price1a*100
gen coc_price1 = coc_price1b/coc_price1a*100
gen meth_price1 = meth_price1b/meth_price1a*100

*full time series w/ heroin price
twoway line her_price1 year, lcolor(black) lw(thick) || ///
line coc_price1 year, lc(black) lp(shortdash) lw(thick) ||	///
line meth_price1 year, lc(gs10) lp(dash) lw(thick)     ///
, yline(100) xlabel(1998(3)2008) ylabel(100(200)500, angle(horizontal))   ///
scheme(s1mono) ytitle("Price relative to year 2000 (= 100)")  ///
 xtitle("Year") xsc(titlegap(2)) ///
 legend(order(1 "Heroin" 2 "Cocaine" 3 "Methamph.") cols(1) ring(0) position(3) )
 
 graph export "results/FigureA1.pdf", replace


 

/*******************************************************************************
********************************************************************************

Figure A2:  Australian Federal Police activities and outcomes related to drug interdiction

********************************************************************************
*******************************************************************************/  


* round policy numbers to years? financial years, so 1996.5 is July 1996 - June 1997

*** A: police funding
import delimited "data/afp_funding.csv", clear
keep if inrange(year,1991,2006)

*overall
*fed_services
*investigation

twoway  (line fed_services year_fin, lcolor(black) lwidth(thick)) ///
 (line investigation year_fin, lcolor(black) lwidth(thick) lpattern(longdash))	///
 , xlabel(1992.5 "1992/93" 1996.5 "1996/97" 2000.5 "2000/01" 2004.5 "2004/05") ///
  ylabel(0(200)800, angle(horizontal) format(%9.0fc)) scheme(s1mono) ///
 xline(1999.5, lc(red)   ) ///
 ytitle("Funding (millions, 2019 dollars)") ///
 ysc(titlegap(1)) xsc(titlegap(2)) xtitle("Australian Financial Years (start 1 July)")  ///
 legend(label(1 "All funding") label(2 "Without terrorism") cols(1) ring(0) pos(4)) 
	
graph export "results/FigureA2a.pdf", replace


*** B: overseas posts
import delimited "data/afp_overseas_posts.csv", clear
keep if inrange(year,1991,2006)

twoway  (line all year, lcolor(black) lwidth(thick)) ///
 (line asiapacific year, lcolor(black) lwidth(thick) lpattern(longdash)) ///
 , xlabel(1992.5 "1992/93" 1996.5 "1996/97" 2000.5 "2000/01" 2004.5 "2004/05") ///
 ylabel(0(5)25, angle(horizontal) format(%9.0fc)) scheme(s1mono) ///
 xline(1999.5, lc(red)   ) ///
 ytitle("Overseas offices")  ///
 ysc(titlegap(0)) xsc(titlegap(2)) xtitle("Australian Financial Years (start 1 July)")  legend(label(1 "All") ///
 label(2 "In Asia-Pacific") cols(1) ring(0) pos(5))  
 
graph export "results/FigureA2b.pdf", replace


*** C: sum of heroin seizures
import delimited "data/afp_heroin_seizures.csv", clear
keep if inrange(year,1989,2006)

gen weight_kg=weight_g / 1000

twoway  (line weight_kg year, lcolor(black) lwidth(thick)) ///
 , xlabel(1992.5 "1992/93" 1996.5 "1996/97" 2000.5 "2000/01" 2004.5 "2004/05") ///
 ylabel(, angle(horizontal) format(%9.0fc)) scheme(s1mono) ///
 xline(1999.5, lc(red)   ) ///
 ytitle("Sum of heroin seizures (kg)")  ///
 ysc(titlegap(0)) xsc(titlegap(2)) xtitle("Australian Financial Years (start 1 July)") ///
 legend(off)  
 
graph export "results/FigureA2c.pdf", replace

*** D: ave. size of heroin seizures
import delimited "data/afp_heroin_seizures.csv", clear
keep if inrange(year,1989,2006)

gen ave_weight_kg=weight_g / (1000 * seizures)

twoway  (line ave_weight_kg year, lcolor(black) lwidth(thick)) ///
 , xlabel(1992.5 "1992/93" 1996.5 "1996/97" 2000.5 "2000/01" 2004.5 "2004/05" ) ///
 ylabel(0(2)6, angle(horizontal) format(%9.0fc)) scheme(s1mono) ///
 xline(1999.5, lc(red)   ) ///
 ytitle("Average weight of heroin seizures (kg)")  ///
 ysc(titlegap(0)) xsc(titlegap(2)) xtitle("Australian Financial Years (start 1 July)") ///
 legend(off)  
 
graph export "results/FigureA2d.pdf", replace




/*******************************************************************************
********************************************************************************

Figure A3: Measures of government services in New South Wales, 1996-2008

********************************************************************************
*******************************************************************************/  
 

**********************
*** NSW data

import delimited "data/annual_data.csv", clear
keep if inrange(year,1996,2008)

* round policy numbers to years? financial years, so 1996.5 is July 1996 - June 1997
gen staff_pc=police_staff/nsw_pop_dec*100000

*** A: police staffing
twoway  (line staff_pc year, lcolor(black) lwidth(thick)) ///
 (line police_officers year, lcolor(black) lwidth(thick) lpattern(longdash))	///
 , xlabel(1997.5 "1997/98" 2000.5 "2000/01" 2003.5 "2003/04" 2006.5 "2006/07") ylabel(150(50)300, angle(horizontal) format(%9.0fc)) ///
 scheme(s1mono)  xline(1999.5, lc(red)) ytitle("Employees per 100,000 pop.") ///
 ysc(titlegap(0)) xsc(titlegap(2)) xtitle("Australian Financial Years (start 1 July)")  ///
 legend(label(1 "All employees") label(2 "Police officers") cols(1) ring(0) pos(5)) 

graph export "results/FigureA3a.pdf", replace


*** B: corrections
gen prison_pop=aveprisonpop/nsw_pop_dec*100000
gen prison_capacity=prisdesigncapacity_total/nsw_pop_dec*100000

twoway  (line prison_pop year, lcolor(black) lwidth(thick)) ///
 (line prison_capacity year, lcolor(black) lwidth(thick) lpattern(longdash)) ///
 , xlabel(1997.5 "1997/98" 2000.5 "2000/01" 2003.5 "2003/04" 2006.5 "2006/07") ylabel(50(50)150, angle(horizontal) format(%9.0fc)) ///
 scheme(s1mono) xline(1999.5, lc(red) ) ytitle("Prisoners per 100,000 pop.")  ///
 ysc(titlegap(0)) xsc(titlegap(2)) xtitle("Australian Financial Years (start 1 July)")  legend(label(1 "Daily average") ///
 label(2 "Prison design capacity") cols(1) ring(0) pos(5))  
 
graph export "results/FigureA3b.pdf", replace


*** C: mental health
twoway  (line mental_ftestaff year, lcolor(black) lwidth(thick)) ///
 (line mental_beds year, lcolor(black) lwidth(thick) lpattern(longdash)) ///
 , xlabel(1997.5 "1997/98" 2000.5 "2000/01" 2003.5 "2003/04" 2006.5 "2006/07") ylabel(25(25)100, angle(horizontal) format(%9.0fc)) ///
 scheme(s1mono) xline(1999.5, lc(red) ) ytitle("Employees/beds per 100,000 pop.")  ///
 ysc(titlegap(0)) xsc(titlegap(2)) xtitle("Australian Financial Years (start 1 July)") legend(label(1 "Employees") ///
 label(2 "Beds provided") cols(1) ring(0) pos(5))  
 
graph export "results/FigureA3c.pdf", replace


*** D: pension payments
import delimited "data/dss_pensions.csv", clear

gen year=year_all
keep if year>=1996 & year<=2008.5

gen scaling=(113.2940647/cpi_notax)
gen disability = disability_single*scaling
gen unemp = unemployment*scaling

twoway (line disability year, lc(black) lp(solid) lw(thick)   ) ///
 (line unemp year, lcolor(black) lwidth(thick) lpattern(longdash)) ///
 , xline(2000.75, lc(red) ) scheme(s1mono) ///
 ytitle("Weekly pension payment (2019 Australian $)") ylabel(240(30)340, angle(horizontal) format(%9.0fc)) ///
 xtitle("")  xlabel(1996(2)2008)  xtick(1996(1)2008) ///
 xsc(titlegap(2)) xtitle("Year") ///
 legend(label(1 "Disability") label(2 "Unemployment") cols(1) ring(0) pos(5))  
graph export "results/FigureA3d.pdf", replace


*** E: unemployment rate & employment/population ratio
import delimited "data/abs_lfp_data.csv", clear

gen date_mdy=mdy(month,1,year)
gen ym=mofd(date_mdy)
gen yq=qofd(date_mdy)

format ym %tm
format yq %tq

keep if yq>=yq(1995,4) & yq<=yq(2008,4)

preserve
collapse (mean) emppop_* urate_*, by(yq)
twoway line urate_t yq, lcolor(black) lw(thick) || ///
 line emppop_all_t yq, lc(black) lp(dash) lw(thick) yaxis(2) ///
 , xline(`=yq(2000,4)', lc(red) ) scheme(s1mono) ///
 ytitle("Unemployment rate (%)") ylabel(3(1)8.4, angle(horizontal) format(%9.0fc)) ///
 ytitle("Employment-to-population ratio (%)", axis(2)) ylabel(56(1)61.4, angle(horizontal) format(%9.0fc)  axis(2)) ///
 xtitle("Year")  xlabel(`=yq(1996,1)' "1996" `=yq(1998,1)' "1998" `=yq(2000,1)' "2000" `=yq(2002,1)' "2002" `=yq(2004,1)' "2004" `=yq(2006,1)' "2006" `=yq(2008,1)' "2008" ) ///
 xtick(`=yq(1996,1)'(4)`=yq(2008,1)') xsc(titlegap(2))  ///
 legend(order(1 "Unemployment rate (on left)" 2 "Employment/pop. (on right)") cols(1) ring(0) position(5) )
 graph export "results/FigureA3e.pdf", replace
restore

 
*** F: Weekly earnings
import delimited "data/pensions_earnings.csv", clear

gen date_mdy=mdy(month,1,year)
gen yq=qofd(date_mdy)

format yq %tq

format
keep if yq>=yq(1995,4) & yq<=yq(2008,4)

gen scaling=(113.2940647/cpi_notax)
gen earnings = ave_earnings*scaling

twoway (line earnings yq, lc(black) lp(solid) lw(thick)   ) ///
 , xline(`=yq(2000,4)', lc(red)  ) scheme(s1mono) ///
 ytitle("Weekly earnings (2019 Australian $)") ylabel(, angle(horizontal)) ///
 xtitle("Year")  xlabel(`=yq(1996,1)' "1996" `=yq(1998,1)' "1998" `=yq(2000,1)' "2000" `=yq(2002,1)' "2002" `=yq(2004,1)' "2004" `=yq(2006,1)' "2006" `=yq(2008,1)' "2008" ) ///
 xtick(`=yq(1996,1)'(4)`=yq(2008,1)') xsc(titlegap(2)) xtitle("Year") ///
 legend(off)  
graph export "results/FigureA3f.pdf", replace
 
 

/*******************************************************************************
********************************************************************************

Figure A4: Severity of punishment for drug, violent and property offenses 
********************************************************************************
*******************************************************************************/  
 
clear
set matsize 10000  
use "data/rod12872_20150706_apr.dta", clear


gen year=year(indexdate)
gen quarter=quarter(indexdate)
gen yq=yq(year,quarter)

sort mspdi indexdate
bys mspdi: egen first_year=min(year)


keep if inrange(yq,yq(1995,4),yq(2008,4))

gen prison=(index_ppencode==1)
gen fine=(index_ppencode==18)
gen other=(prison==0 & fine==0)
*what type of "other" is going up for opiates? 
gen suspsentence=inrange(index_ppencode,5,9)
gen communityservice=index_ppencode==10
gen bond=inlist(index_ppencode,12,13,19,20,23)

gen prison_length=index_ppenval if index_ppencode==1
gen fine_amt=index_ppenval if index_ppencode==18

gen anzsoc=index_poanzsoc


gen off_violent=inrange(anzsoc,100,699)
gen off_property=inrange(anzsoc,700,999)
gen off_robbery=inrange(anzsoc,610,619)
*non-robbery violent
gen off_violent_nonrobb=off_violent - off_robbery
*property including robbery
gen off_property_robb=off_property + off_robbery

gen off_drug=inrange(anzsoc,1000,1099)
gen off_drug_use=(inlist(anzsoc,1041,1042,1022))
gen off_drug_sale=(inlist(anzsoc,1031,1032,1011,1012,1021))

gen off_drug_opiates=(off_drug==1 & index_podrugtype==1)
gen off_drug_opiates_use=(off_drug_use==1 & index_podrugtype==1)
gen off_drug_opiates_sale=(off_drug_sale==1 & index_podrugtype==1)

gen off_drug_amph=(off_drug==1 & index_podrugtype==4)
gen off_drug_amph_use=(off_drug_use==1 & index_podrugtype==4)
gen off_drug_amph_sale=(off_drug_sale==1 & index_podrugtype==4)

gen off_drug_cocaine=(off_drug==1 & index_podrugtype==6)
gen off_drug_cocaine_use=(off_drug_use==1 & index_podrugtype==6)
gen off_drug_cocaine_sale=(off_drug_sale==1 & index_podrugtype==6)

gen off_type=""
replace off_type="off_violent_nonrobb" if off_violent_nonrobb==1
replace off_type="off_property_robb" if off_property_robb==1
replace off_type="off_drug_opiates_use" if off_drug_opiates_use==1
replace off_type="off_drug_opiates_sale" if off_drug_opiates_sale==1
replace off_type="off_drug_amph_use" if off_drug_amph_use==1
replace off_type="off_drug_amph_sale" if off_drug_amph_sale==1
replace off_type="off_drug_cocaine_use" if off_drug_cocaine_use==1
replace off_type="off_drug_cocaine_sale" if off_drug_cocaine_sale==1


drop if yq<yq(1995,4) 
drop if yq>yq(2008,4)

gen Ayear=.
forvalues q=1995/2000 {
replace Ayear=`q' if ((year==`q' & 	inrange(quarter(dofq(yq)),1,3)) | (year==`q'-1 & quarter(dofq(yq))==4))
}

replace Ayear=2000 if yq==yq(2000,4)

forvalues q=2001/2008 {
replace Ayear=`q' if year==`q'
}

capture drop Ayear_*
forvalues i=1996/2008 {
gen Ayear_`i'=(Ayear==`i')
}
drop Ayear_2000

gen q4y2000=yq==yq(2000,4)

replace agefirst=99 if agefirst==.

*top cap prison sentence at 20 years
replace prison_length=240 if prison_length>240 & prison_length~=.

*top cap prior and concurrent charges at 5
foreach var of varlist nprior* index_concurr index_hom-index_brviol {
replace `var'=5 if `var'>=5 & `var'~=.
}


global dem "i.age i.gender i.indig  i.lga"
global crim "i.index_polawpart i.agefirst i.nprior_court i.nprior_pris i.quarter"
global concurr "i.index_concurr i.index_actsinj i.index_assault i.index_serassault i.index_sexassrel i.index_sexassault i.index_dangneg i.index_abdhar i.index_robrel i.index_rob i.index_brent i.index_theft i.index_fraud i.index_drug i.index_weap i.index_propdam i.index_pubord i.index_traff i.index_drive i.index_pca i.index_just i.index_brcust i.index_brcomm i.index_brviol"

gen off_harddrug=off_drug_amph+off_drug_cocaine

foreach depvar in "off_drug_opiates"  "off_harddrug" "off_property_robb" "off_violent_nonrobb"    {
foreach v in "prison"  {
reg `v' Ayear_* q4y2000 i.quarter $dem $crim $concurr if `depvar'==1 & convict==2 & jd=="L" & age>=18,  vce(cluster mspdi)

 sum `v' if inrange(yq,yq(1999,4),yq(2000,3)) & e(sample)==1
 local mean=r(mean)
 
regsave Ayear_* using "data/`depvar'_penalty_full.dta" , replace addvar(Ayear_2000,0,0) ///
 addlab(outcome, "`v'", mean, `mean')  ci level(95) 
}
foreach v in "fine"  {
reg `v' Ayear_* q4y2000 i.quarter $dem $crim $concurr if `depvar'==1 & convict==2 & jd=="L" & age>=18,  vce(cluster mspdi)

 sum `v' if inrange(yq,yq(1999,4),yq(2000,3)) & e(sample)==1
 local mean=r(mean)
 
regsave Ayear_* using "data/`depvar'_penalty_full.dta" , append addvar(Ayear_2000,0,0) ///
 addlab(outcome, "`v'", mean, `mean')  ci level(95) 
}

}

preserve
clear
set obs 1
gen start=.
save "data/prison_length_full.dta", replace
restore

preserve
clear
set obs 1
gen start=.
save "data/fine_amt_full.dta", replace
restore

foreach depvar in "off_drug_opiates"  "off_harddrug" "off_property_robb" "off_violent_nonrobb"    {
foreach v in "prison_length" "fine_amt"  {
reg `v' Ayear_* q4y2000 i.quarter $dem $crim $concurr if `depvar'==1 & convict==2 & jd=="L" & age>=18,  vce(cluster mspdi)

 sum `v' if inrange(yq,yq(1999,4),yq(2000,3)) & e(sample)==1
 local mean=r(mean)
 
regsave Ayear_* using "data/`v'_full.dta" , append addvar(Ayear_2000,0,0) ///
 addlab(outcome, "`v'", mean, `mean', depvar, "`depvar'")  ci level(95) 
}
} 


use "data/off_drug_opiates_penalty_full.dta", clear

gen year=substr(var,7,4) 

 destring year, replace
gen time=year - 2001 


foreach var of varlist coef ci_upper ci_lower {
gen sc_`var'=`var'+mean
}

sort outcome time
 
twoway (line sc_coef time if outcome=="prison", lc(black) lp(solid) lw(thick))  ///
(line sc_coef time if outcome=="fine", lc(black) lp(dash)lw(thick)) ///
, xline(-1 , lc(red)  )  ///
scheme(s1mono)   ylabel(0(.1)1, angle(horizontal) format(%9.1fc) )  ///
xlabel(-5(1)7) xtitle("Years (relative to onset)") xsc(titlegap(1)) ///
 ytitle("Sentence type") ysc(titlegap(0)) ///
 legend(order(1 "Incarceration" 2 "Fine" ) cols(1) ring(0) pos(2))

  graph export "results/FigureA4a.pdf", replace

  
use "data/off_harddrug_penalty_full.dta", clear

gen year=substr(var,7,4) 

 destring year, replace
gen time=year - 2001 


foreach var of varlist coef ci_upper ci_lower {
gen sc_`var'=`var'+mean
}

sort outcome time
 
twoway (line sc_coef time if outcome=="prison", lc(black) lp(solid) lw(thick))  ///
(line sc_coef time if outcome=="fine", lc(black) lp(dash)lw(thick)) ///
, xline(-1 , lc(red)  )  ///
scheme(s1mono)   ylabel(0(.1)1, angle(horizontal) format(%9.1fc) )  ///
xlabel(-5(1)7) xtitle("Years (relative to onset)") xsc(titlegap(1)) ///
 ytitle("Sentence type") ysc(titlegap(0)) ///
 legend(order(1 "Incarceration" 2 "Fine" ) cols(1) ring(0) pos(2))

  graph export "results/FigureA4b.pdf", replace

  
use "data/off_violent_nonrobb_penalty_full.dta", clear

gen year=substr(var,7,4) 

 destring year, replace
gen time=year - 2001 


foreach var of varlist coef ci_upper ci_lower {
gen sc_`var'=`var'+mean
}

sort outcome time
 
twoway (line sc_coef time if outcome=="prison", lc(black) lp(solid) lw(thick))  ///
(line sc_coef time if outcome=="fine", lc(black) lp(dash)lw(thick)) ///
, xline(-1 , lc(red)  )  ///
scheme(s1mono)   ylabel(0(.1)1, angle(horizontal) format(%9.1fc) )  ///
xlabel(-5(1)7) xtitle("Years (relative to onset)") xsc(titlegap(1)) ///
 ytitle("Sentence type") ysc(titlegap(0)) ///
 legend(order(1 "Incarceration" 2 "Fine" ) cols(1) ring(0) pos(2))

  graph export "results/FigureA4c.pdf", replace

  
use "data/off_property_robb_penalty_full.dta", clear

gen year=substr(var,7,4) 

 destring year, replace
gen time=year - 2001 


foreach var of varlist coef ci_upper ci_lower {
gen sc_`var'=`var'+mean
}

sort outcome time
 
twoway (line sc_coef time if outcome=="prison", lc(black) lp(solid) lw(thick))  ///
(line sc_coef time if outcome=="fine", lc(black) lp(dash)lw(thick)) ///
, xline(-1 , lc(red)  )  ///
scheme(s1mono)   ylabel(0(.1)1, angle(horizontal) format(%9.1fc) )  ///
xlabel(-5(1)7) xtitle("Years (relative to onset)") xsc(titlegap(1)) ///
 ytitle("Sentence type") ysc(titlegap(0)) ///
 legend(order(1 "Incarceration" 2 "Fine" ) cols(1) ring(0) pos(2))

  graph export "results/FigureA4d.pdf", replace
 

use "data/prison_length_full.dta", clear

gen year=substr(var,7,4) 

 destring year, replace
gen time=year - 2001 


foreach var of varlist coef ci_upper ci_lower {
gen sc_`var'=`var'+mean
}

sort outcome time
 
twoway (line sc_coef time if depvar=="off_drug_opiates", lc(black) lp(solid) lw(thick))  ///
(line sc_coef time if depvar=="off_harddrug", lc(black) lp(dash) lw(thick)) ///
(line sc_coef time if depvar=="off_violent_nonrobb", lc(gs8) lp(solid) lw(thick)) ///
(line sc_coef time if depvar=="off_property_robb", lc(gs8) lp(dash) lw(thick)) ///
, xline(-1 , lc(red)  )  ///
scheme(s1mono)   ylabel(2(2)8, angle(horizontal) format(%9.0fc) )  ///
xlabel(-5(1)7) xtitle("Years (relative to onset)") xsc(titlegap(1)) ///
 ytitle("Incarceration sentence length (months)") ysc(titlegap(0)) ///
 legend(order(1 "Opioids" 2 "Non-opioid drugs" 3 "Violent" 4 "Property") cols(1) ring(0) pos(5))

  graph export "results/FigureA4e.pdf", replace


use "data/fine_amt_full.dta", clear

gen year=substr(var,7,4) 

 destring year, replace
gen time=year - 2001 


foreach var of varlist coef ci_upper ci_lower {
gen sc_`var'=`var'+mean
}

sort outcome time
 
twoway (line sc_coef time if depvar=="off_drug_opiates", lc(black) lp(solid) lw(thick))  ///
(line sc_coef time if depvar=="off_harddrug", lc(black) lp(dash) lw(thick)) ///
(line sc_coef time if depvar=="off_violent_nonrobb", lc(gs8) lp(solid) lw(thick)) ///
(line sc_coef time if depvar=="off_property_robb", lc(gs8) lp(dash) lw(thick)) ///
, xline(-1 , lc(red)  )  ///
scheme(s1mono)   ylabel(100(100)600, angle(horizontal) format(%9.0fc) )  ///
xlabel(-5(1)7) xtitle("Years (relative to onset)") xsc(titlegap(1)) ///
 ytitle("Fine amount (AUD)") ysc(titlegap(0)) ///
 legend(order(1 "Opioids" 2 "Non-opioid drugs" 3 "Violent" 4 "Property") cols(1) ring(0) pos(5))

  graph export "results/FigureA4f.pdf", replace


  
/*******************************************************************************
********************************************************************************
Figure A5: Opioid-related mortality 
********************************************************************************
*******************************************************************************/  
   
use "data/panel.dta", clear

collapse (mean) mortality_opiates2, by(yq)

gen year = year(dofq(yq))

collapse (mean) mortality_opiates2, by(year)
*average quarterly ortality rate

gen sample_mortality_opiates=mortality_opiates2*4*100000

preserve

import delimited "data/us_nsw_opioids_comparison.csv", clear 
gen year=nsw_year
drop if year>2008
drop if year<1995

keep year nsw_deathr
tempfile nsw
save `nsw'

restore 

merge 1:1 year using `nsw'
drop _merge

twoway (line nsw_deathr year, lcolor(black) lw(thick)) || ///
line sample_mortality_opiates year, lcolor(gs10) lw(thick) lp(shortdash) yaxis(2) ///
, xlabel(1996(2)2008) ylabel(, angle(horizontal))   ///
scheme(s1mono) ytitle("Deaths per 100,000 (population)")  ///
ytitle("Deaths per 100,000 (est. sample)", axis(2))  ///
 xtitle("Year") xsc(titlegap(2)) ///
 legend(order(1 "NSW total population" "opioid-related mortality rate" "(left axis)"  2 "Estimation sample" "opioid-related mortality rate" "(right axis)") ///
 holes(4 5) cols(1) ring(0) position(2) symxsize(small) )
 
 graph export "results/FigA5.pdf", replace

 
 
 
********************************************************************************
********************************************************************************
*Figure A.6: Other outcomes
********************************************************************************
********************************************************************************


use "data/panel.dta", clear

global depvars "off_driving off_drug_hard_nonO_sale  off_assault_seriousinj  off_assault_seriousnoninj "

foreach var of varlist $depvars {

preserve 
collapse (sum) `var' (count) obs=ppn, by(yq treated)
gen R`var'=1000*4*(`var'/obs)
keep R`var' yq treated
reshape wide R`var', i(yq) j(treated)
format yq %tq

cap drop fakeline
gen fakeline=.

drop if yq<=yq(1995,3)

twoway (line R`var'1 yq, lc(black) lp(solid) lw(thick)) (line R`var'0 yq, lc(gs4) lp(shortdash) lw(thick)) ///
(scatter fakeline yq, mc(red) msymbol(|) msize(large) ) ///
,  xline(`=yq(2000,4)', lc(red)  lw(thick)   ) scheme(s1mono) ///
ytitle("Rate per 1000") ylabel(, angle(horizontal) format(%9.0fc)) ///
xtitle("")  xlabel(`=yq(1996,4)'(8)`=yq(2008,1)') ///
 xtick(`=yq(1996,4)'(4)`=yq(2008,1)') ///
  legend(off) 
graph export "results/FigureA6_RAW_`var'.pdf", replace 
 
restore
}


global indcontrols "treated i.female i.indig i.female_missing i.indig_missing  "
global agecontrols "i.age_int  i.age_int#i.female i.age_int#i.indig "
global offcontrols "i.first_drug_off_ym"

global depvars "off_driving off_drug_hard_nonO_sale  off_assault_seriousinj  off_assault_seriousnoninj "

foreach var of varlist $depvars {
	
cap drop Ayear_2000 Atyear_2000

capture drop R`var' 
gen R`var'=`var'*1000*4

tempfile coeff

areg R`var' $indcontrols $agecontrols $offcontrols ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr  , absorb(ppn) vce(cluster ppn)
regsave using `coeff', addvar(Atyear_2000,0,0) ci replace

preserve
use `coeff', clear
keep var coef ci_lower ci_upper
gen year=substr(var,8,4)
gen grp=substr(var,1,2)
destring year, replace force
keep if grp=="At"
keep if year>=1995 & year<=2008
replace ci_lower=0 if year==2000
replace ci_upper=0 if year==2000
drop var
sort year
gen time=year - 2001 

rename coef E`var'
rename ci_lower E`var'_l
rename ci_upper E`var'_u

cap drop fakeline
gen fakeline=.

*event study
twoway (line E`var' time, lc(black) lp(solid) lw(medthick)) ///
 (rarea E`var'_l E`var'_u time, color(gs12%50) lp(shortdash) lc(black) ) ///
 (scatter fakeline time, mc(red) msym(|)) ///
 , yline(0) xline(-1, lc(red)) scheme(s1mono) ///
 xlabel(-5(1)7) xtitle("Years (relative to onset)") xsc(titlegap(1)) ///
 ylabel(, angle(horizontal)) ytitle("Estimates") ysc(titlegap(0)) ///
 legend(off)  
 graph export "results/FigureA6_ES_`var'.pdf", replace 

restore
}






/*******************************************************************************
********************************************************************************

Figure A7: Estimates applied to overall NSW criminal charges

********************************************************************************
*******************************************************************************/  
 
*coefficients for property violent and drug use

use "data/panel.dta", clear

keep if inrange(Ayear,1996,2008)

gen off_drug_use= off_drug_opiates_use + off_drug_cocaine_use + off_drug_amph_use + off_drug_other_use
gen off_all_nontraffic=off_all-off_traffic

*gen off_property_serious=off_breakenter+off_mv_theft+off_robbery


global agvars "off_drug_opiates_use  off_violent_nonrobb "
foreach var of varlist $agvars {

capture drop R`var' 
gen R`var'=`var'*1000*4

areg R`var' $indcontrols $agecontrols $offcontrols ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr , absorb(ppn) vce(cluster ppn)


regsave Atyear_2001 Atyear_2002 Atyear_2003 Atyear_2004 Atyear_2005 Atyear_2006 Atyear_2007 Atyear_2008 ///
 using "results/coef_`var'_temp.dta", addvar(Atyear_2000,0,0) addlabel(n_treat, 26464) ci replace
 
}

global agvars2 "off_property_robb  "
foreach var of varlist $agvars2 {

capture drop R`var' 
gen R`var'=`var'*1000*4

areg R`var' $indcontrols $agecontrols $offcontrols ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr ///
if (treated==1 | (treated==0  & (pre_off_breakenter>0 | pre_off_mv_theft>0 | pre_off_robbery>0))) ///
, absorb(ppn) vce(cluster ppn)

tempfile `coeff_`var''

regsave Atyear_2001 Atyear_2002 Atyear_2003 Atyear_2004 Atyear_2005 Atyear_2006 Atyear_2007 Atyear_2008 ///
 using "results/coef_`var'_temp.dta", addvar(Atyear_2000,0,0) addlabel(n_treat, 26464) ci replace
 
}



foreach var in  $agvars $agvars2 {
use "results/coef_`var'_temp.dta", clear
keep var coef ci_lower ci_upper n_treat
gen year=substr(var,8,4)
destring year, replace force
gen `var'_chg=(coef/1000)*6886
keep year  `var'_chg
tempfile d`var'
save `d`var''
}
*global agvars "off_drug_use off_drug_opiates_use off_drug_cocaine_use off_drug_amph_use off_violent_nonrobb off_property_robb off_all_nontraffic"

use `doff_drug_opiates_use', clear

merge 1:1 year using `doff_violent_nonrobb'
drop _merge
merge 1:1 year using `doff_property_robb'
drop _merge

tempfile coef
save `coef'



*total ROD charge data
use "data/rod12872_20150706_off.dta", clear

gen off_violent=inrange(anzsoc,100,699)
gen off_property=inrange(anzsoc,700,999)

gen off_robbery=inrange(anzsoc,610,619)
gen off_breakenter=inrange(anzsoc,710,719)
gen off_theft=inrange(anzsoc,800,899)
gen off_fraud=inrange(anzsoc,900,999)
gen off_propdamage=inrange(anzsoc,1210,1219)
gen off_puborder=inrange(anzsoc,1300,1399)
gen off_traffic=inrange(anzsoc,1400,1499)
gen off_mv_theft=inrange(anzsoc,811,812)


gen off_drug_use=(inlist(anzsoc,1041,1042,1022))

gen off_murder=inrange(anzsoc,100,111) 
*murder plus includes attempted and manslaughter charges and driving causing death
gen off_murder_plus=inrange(anzsoc,100,199)

gen drugtypestr="opiates" if drugtype==1
replace drugtypestr="amph" if drugtype==4
replace drugtypestr="cocaine" if drugtype==6


foreach t in "amph" "cocaine" "opiates"   {
gen off_drug_`t'=drugtypestr=="`t'"
gen off_drug_`t'_use=(drugtypestr=="`t'" & inlist(anzsoc,1041,1042,1022))
gen off_drug_`t'_sale=(drugtypestr=="`t'" & inlist(anzsoc,1031,1032,1021,1011,1012))

}

gen off_all=1

drop if drugtype==2 // exclude cannabis

*non-robbery violent
gen off_violent_nonrobb=off_violent - off_robbery
*property including robbery
gen off_property_robb=off_property + off_robbery
gen off_property_serious=off_breakenter+off_mv_theft+off_robbery
gen off_theft_nonmv=off_theft - off_mv_theft


gen off_all_nontraffic=off_all-off_traffic

gen year=year(offdate)
keep if inrange(year, 1995, 2008)

collapse (sum) $agvars $agvars2, by(year)

merge 1:1 year using `coef'
drop _merge

foreach var in $agvars $agvars2 {
gen C`var'=`var'-`var'_chg
replace C`var'=`var' if year==2000
}

preserve
insheet using "data/nsw_pop_age.csv", clear
foreach var of varlist totalpop {
replace `var'=subinstr(`var',",","",.)
}
foreach var of varlist totalpop {
destring `var', replace
}
keep if inrange(year,1995,2008)
keep if agecat=="All ages"
drop agecat
tempfile pop
save `pop'
restore

merge 1:1 year using `pop'

foreach var in  $agvars $agvars2 {
gen `var'_p10k=(`var'/totalpop)*10000
gen C`var'_p10k=(C`var'/totalpop)*10000
gen diff_`var'=`var'_p10k-C`var'_p10k
}


twoway ///
line off_drug_opiates_use_p10k year, lcolor(black) lw(thick) || ///
line Coff_drug_opiates_use_p10k year, lcolor(gs10) lw(thick) lp(shortdash) ///
, xlabel(1996(2)2008) ylabel(1(1)5, angle(horizontal))   ///
scheme(s1mono) ytitle("Number of criminal charges per 10,000 pop")  ///
 xtitle("Year") xsc(titlegap(2)) ///
 legend(order(1 "Actual charges"  2 "Removing effects" "from our sample") ///
 holes(2) cols(1) ring(0) position(10) symxsize(med) )
 
 graph export "results/FigureA7a.pdf", replace
 

twoway ///
line off_violent_nonrobb_p10k year, lcolor(black) lw(thick) || ///
line Coff_violent_nonrobb_p10k year, lcolor(gs10) lw(thick) lp(shortdash) ///
, xlabel(1996(2)2008) ylabel(, angle(horizontal))   ///
scheme(s1mono) ytitle("Number of criminal charges per 10,000 pop")  ///
 xtitle("Year") xsc(titlegap(2)) ///
 legend(order(1 "Actual charges"  2 "Removing effects" "from our sample") ///
 holes(2) cols(1) ring(0) position(10) symxsize(med) )
 
 graph export "results/FigureA7b.pdf", replace
 

 twoway ///
line off_property_robb_p10k year, lcolor(black) lw(thick) || ///
line Coff_property_robb_p10k year, lcolor(gs10) lw(thick) lp(shortdash) ///
, xlabel(1996(2)2008) ylabel(, angle(horizontal))   ///
scheme(s1mono) ytitle("Number of criminal charges per 10,000 pop")  ///
 xtitle("Year") xsc(titlegap(2)) ///
 legend(order(1 "Actual charges"  2 "Removing effects" "from our sample") ///
 holes(2) cols(1) ring(0) position(10) symxsize(med) )
 
 graph export "results/FigureA7c.pdf", replace
 
erase "results/coef_off_drug_opiates_use_temp.dta"
erase "results/coef_off_violent_nonrobb_temp.dta"
erase "results/coef_off_property_robb_temp.dta"




****************************************************************************
****************************************************************************
****************************************************************************
** Table A.1: Mean sample characteristics prior to heroin supply reduction
****************************************************************************
****************************************************************************
****************************************************************************


use "data/panel.dta", clear

*create indicator for in estimation sample to create summary statistics
global indcontrols "treated i.female i.indig i.female_missing i.indig_missing  "
global agecontrols "i.age_int  i.age_int#i.female i.age_int#i.indig "
global offcontrols "i.first_drug_off_ym"
foreach var of varlist mortality {
capture drop R*
gen R`var'=`var'*1000*4

reg R`var' $indcontrols $agecontrols $offcontrols ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr  ,  vce(cluster ppn)
}
gen regsample=e(sample)

capture drop R*
foreach var of varlist off_* mortality* phdas_yq  {
capture drop R`var'
gen R`var'=`var'*4*1000
}

est clear

global sumstats "age female Rmortality Rmortality_opiates Rmortality_otherdrugsalc Rmortality_nondrugsalc Roff_drug_opiates_use Roff_drug_hard_nonO_use Roff_drug_cocaine_use Roff_drug_amph_use Roff_drug_opiates_sale Roff_drug_hard_nonO_sale Roff_violent_nonrobb Roff_murder_plus Roff_assault_serious Roff_sexassault Roff_assault_common Roff_harrassment Roff_property Roff_robbery Roff_breakenter Roff_mv_theft Roff_theft_nonmv"  
estpost su $sumstats if treated==1 & regsample==1 & inrange(Ayear,1996,2000)
preserve
keep if treated==1 & regsample==1
egen calcind=group(ppn) 
sum calcind
estadd scalar N_ind=r(max)
drop calcind
est store T
restore

estpost su $sumstats if treated==0 & regsample==1 & inrange(Ayear,1996,2000)
preserve
keep if treated==0 & regsample==1
egen calcind=group(ppn) 
sum calcind
estadd scalar N_ind=r(max)
drop calcind
est store C
restore

estpost su $sumstats if treated==0  & (pre_off_breakenter>0 | pre_off_mv_theft>0 | pre_off_robbery>0) & regsample==1 & inrange(Ayear,1996,2000)
preserve
keep if treated==0 & (pre_off_breakenter>0 | pre_off_mv_theft>0 | pre_off_robbery>0) & regsample==1
egen calcind=group(ppn) 
sum calcind
estadd scalar N_ind=r(max)
drop calcind
est store C2
restore

label var age "Age"
label var female "Female"

*annualized rates 
label var Rmortality "Mortality" 
label var Rmortality_opiates "\;\;\;Opioid-related mortality"
label var Rmortality_otherdrugsalc "\;\;\;Non-opoid drug or alchol related mortality"
label var Rmortality_nondrugsalc "\;\;\;Mortality not related to drugs or alcohol"
label var Roff_drug_opiates_use "\;\;\;Opioid use/possession"
label var Roff_drug_hard_nonO_use "\;\;\;Non-opioid illicit drug use/possession"
label var Roff_drug_cocaine_use "\;\;\;Cocaine use/possession"
label var Roff_drug_amph_use "\;\;\;Methamphetamine use/possession"
label var Roff_drug_opiates_sale "\;\;\;Opioid supply/distribution"
label var Roff_drug_hard_nonO_sale "\;\;\;Non-opioid illicit drug sale/distribution"
label var Roff_violent_nonrobb "Violent offending"
label var Roff_murder_plus "\;\;\;Homicide and manslaughter"
label var Roff_assault_serious "\;\;\;Serious assault"
label var Roff_sexassault "\;\;\;Sexual assault"
label var Roff_assault_common "\;\;\;Common assault"
label var Roff_harrassment "\;\;\;Harassment"
label var Roff_property "Property offending"
label var Roff_property_serious "Property offending"
label var Roff_property_robb "Property offending"
label var Roff_robbery "\;\;\;Robbery"
label var Roff_breakenter "\;\;\;Burglary/break \& enter"
label var Roff_mv_theft "\;\;\;Motor vehicle theft"
label var Roff_theft_nonmv "\;\;\;Petty/other theft"

local titles "& \multicolumn{1}{c}{\specialcell{Opioid \\ drug \\ offenders \\ \;  }}   & \multicolumn{1}{c}{\specialcell{Non-opioid \\ drug \\ offenders \\ \;   }} & \multicolumn{1}{c}{\specialcell{Non-opioid \\ drug \& \\ property \\ offenders }}  \\ "
*local titles2 "& \multicolumn{4}{c}{} & \multicolumn{3}{c}{\specialcell{\textit{{\tiny $^{\text{A}}$ excl. crackdown areas}}}}  \\"
local numbers "\cmidrule(lr){2-4}  \\ "

esttab  T C C2  using "results/TableA1.tex", replace ///
refcat(age "\underline{Demographic characteristics}" ///
Rmortality "\underline{Annualized incidence rates per 1000 individuals}" ///
Roff_drug_opiates_use "Illicit drug offending:" /// 
, nolabel) ///
mlabels(none) nonumbers posthead("`titles'"   "`numbers'") ///
cells(mean(fmt(a2)) ) label booktabs nonum collabels(none) nogaps nolines f ///
stats(N_ind , fmt(%9.0fc   ) ///
layout( "\multicolumn{1}{c}{@}"  )  ///
labels("\midrule No. of unique individuals"   )) 

 
****************************************************************************
****************************************************************************
****************************************************************************
** Table A.2: Estimated changes in other outcomes (per 1,000 individuals)
****************************************************************************
****************************************************************************
****************************************************************************   
   

use "data/panel.dta", clear

gen off_drug_hardsale=off_drug_hard_nonO_sale
gen off_drug_sale=off_drug_hardsale + off_drug_opiates_sale

gen mortality_ndalc2=mortality_nondrugsalc2
gen off_assault_sinj=off_assault_seriousinj
gen off_assault_sninj=off_assault_seriousnoninj
gen off_drug_cuse=off_drug_cannabis_use
gen off_drug_csale=off_drug_cannabis_sale

gen mortality_odalc2=mortality_otherdrugsalc2

global depvarsnofeapp "mortality_otherdrugs2 mortality_odalc2 mortality_ndalc2 mortality_suicide"
global depvarsfeapp " off_driving_alcohol  off_driving_death off_drug_sale off_drug_opiates_sale off_drug_hardsale  off_assault_sinj off_assault_sninj "

global indcontrols "treated i.female i.indig i.female_missing i.indig_missing  "
global agecontrols "i.age_int  i.age_int#i.female i.age_int#i.indig "
global offcontrols "i.first_drug_off_ym"

global coeftypes "pre post1 post2 post3"


*** Table - no ind. FE
foreach var of varlist $depvarsnofeapp  {
capture drop R*
gen R`var'=`var'*1000*4

* Baseline - opioid outcomes

reg R`var' $indcontrols $agecontrols $offcontrols Atyear_*  Ayear_* q4y2000 q4y2000_T i.qtr treated ,  vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
(posttotal: ((_b[Atyear_2001] + _b[Atyear_2002] + _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/8)) ///
, post 

foreach c of global coeftypes {
global b_`var'_`c': di %6.2fc _b[`c']
global se_`var'_`c': di %6.2fc _se[`c']
test `c'=0
global `var'_`c'_p: di %12.2fc r(p)
global `var'_`c'_star=cond(${`var'_`c'_p}<.01,"**",cond(${`var'_`c'_p}<.05,"*",""))
}
sum R`var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1 & e(sample)
global ymean_`var': di %6.2fc r(mean)
}

*with individual FE
foreach var of varlist $depvarsfeapp  {
capture drop R*
gen R`var'=`var'*1000*4

areg R`var' $indcontrols $agecontrols $offcontrols Atyear_*  Ayear_* q4y2000 q4y2000_T i.qtr , absorb(ppn)  vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
(posttotal: ((_b[Atyear_2001] + _b[Atyear_2002] + _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/8)) ///
, post 

foreach c of global coeftypes {
global b_`var'_`c': di %6.2fc _b[`c']
global se_`var'_`c': di %6.2fc _se[`c']
test `c'=0
global `var'_`c'_p: di %12.2fc r(p)
global `var'_`c'_star=cond(${`var'_`c'_p}<.01,"**",cond(${`var'_`c'_p}<.05,"*",""))
}
sum R`var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1 & e(sample)
global ymean_`var': di %6.2fc r(mean)
}


texdoc init "results/TableA2.tex", replace force
tex & & \multicolumn{8}{c}{\specialcell{Coefficients (standard errors)}} \\
tex \cmidrule(lr){3-10} \multicolumn{1}{l}{\specialcell{\; \\ \; \\ \; \\ Outcome}} &  \multicolumn{1}{c}{\specialcell{Avg. for \\ treatment \\ group \\ year -1}} & \multicolumn{2}{c}{\specialcell{\; \\ \; \\ Years \\ -5 to -2}} &  \multicolumn{2}{c}{\specialcell{\; \\ \; \\ Year \\ 0}} & \multicolumn{2}{c}{\specialcell{\; \\ \; \\ Year \\ 1}} &  \multicolumn{2}{c}{\specialcell{\; \\ \; \\ Years \\ 2 to 7}}   \\ \hline
tex \\
tex & & \multicolumn{8}{c}{\specialcell{A. Other Mortality outcomes}} \\
tex \\
tex Non-opioid drug related & \multicolumn{1}{c}{${ymean_mortality_otherdrugs2}} &  \multicolumn{1}{c}{ ${b_mortality_otherdrugs2_pre}${mortality_otherdrugs2_pre_star} } &  \multicolumn{1}{c}{(${se_mortality_otherdrugs2_pre})} &  \multicolumn{1}{c}{ ${b_mortality_otherdrugs2_post1}${mortality_otherdrugs2_post1_star} } &  \multicolumn{1}{c}{(${se_mortality_otherdrugs2_post1})} &  \multicolumn{1}{c}{ ${b_mortality_otherdrugs2_post2}${mortality_otherdrugs2_post2_star} } &  \multicolumn{1}{c}{(${se_mortality_otherdrugs2_post2})} &  \multicolumn{1}{c}{ ${b_mortality_otherdrugs2_post3}${mortality_otherdrugs2_post3_star} } &  \multicolumn{1}{c}{(${se_mortality_otherdrugs2_post3})}   \\
tex Non-opioid drug or alcohol related  & \multicolumn{1}{c}{${ymean_mortality_odalc2}} &  \multicolumn{1}{c}{ ${b_mortality_odalc2_pre}${mortality_odalc2_pre_star} } &  \multicolumn{1}{c}{(${se_mortality_odalc2_pre})} &  \multicolumn{1}{c}{ ${b_mortality_odalc2_post1}${mortality_odalc2_post1_star} } &  \multicolumn{1}{c}{(${se_mortality_odalc2_post1})} &  \multicolumn{1}{c}{ ${b_mortality_odalc2_post2}${mortality_odalc2_post2_star} } &  \multicolumn{1}{c}{(${se_mortality_odalc2_post2})} &  \multicolumn{1}{c}{ ${b_mortality_odalc2_post3}${mortality_odalc2_post3_star} } &  \multicolumn{1}{c}{(${se_mortality_odalc2_post3})}   \\
tex Not drug nor alcohol related & \multicolumn{1}{c}{${ymean_mortality_ndalc2}} &  \multicolumn{1}{c}{ ${b_mortality_ndalc2_pre}${mortality_ndalc2_pre_star} } &  \multicolumn{1}{c}{(${se_mortality_ndalc2_pre})} &  \multicolumn{1}{c}{ ${b_mortality_ndalc2_post1}${mortality_ndalc2_post1_star} } &  \multicolumn{1}{c}{(${se_mortality_ndalc2_post1})} &  \multicolumn{1}{c}{ ${b_mortality_ndalc2_post2}${mortality_ndalc2_post2_star} } &  \multicolumn{1}{c}{(${se_mortality_ndalc2_post2})} &  \multicolumn{1}{c}{ ${b_mortality_ndalc2_post3}${mortality_ndalc2_post3_star} } &  \multicolumn{1}{c}{(${se_mortality_ndalc2_post3})}   \\
tex Suicide & \multicolumn{1}{c}{${ymean_mortality_suicide}} &  \multicolumn{1}{c}{ ${b_mortality_suicide_pre}${mortality_suicide_pre_star} } &  \multicolumn{1}{c}{(${se_mortality_suicide_pre})} &  \multicolumn{1}{c}{ ${b_mortality_suicide_post1}${mortality_suicide_post1_star} } &  \multicolumn{1}{c}{(${se_mortality_suicide_post1})} &  \multicolumn{1}{c}{ ${b_mortality_suicide_post2}${mortality_suicide_post2_star} } &  \multicolumn{1}{c}{(${se_mortality_suicide_post2})} &  \multicolumn{1}{c}{ ${b_mortality_suicide_post3}${mortality_suicide_post3_star} } &  \multicolumn{1}{c}{(${se_mortality_suicide_post3})}   \\
tex \\
tex & & \multicolumn{8}{c}{\specialcell{B. Other drug outcomes}} \\
tex \\
tex \specialcell{Impaired driving \\ alcohol-related} & \multicolumn{1}{c}{${ymean_off_driving_alcohol}} &  \multicolumn{1}{c}{ ${b_off_driving_alcohol_pre}${off_driving_alcohol_pre_star} } &  \multicolumn{1}{c}{(${se_off_driving_alcohol_pre})} &  \multicolumn{1}{c}{ ${b_off_driving_alcohol_post1}${off_driving_alcohol_post1_star} } &  \multicolumn{1}{c}{(${se_off_driving_alcohol_post1})} &  \multicolumn{1}{c}{ ${b_off_driving_alcohol_post2}${off_driving_alcohol_post2_star} } &  \multicolumn{1}{c}{(${se_off_driving_alcohol_post2})} &  \multicolumn{1}{c}{ ${b_off_driving_alcohol_post3}${off_driving_alcohol_post3_star} } &  \multicolumn{1}{c}{(${se_off_driving_alcohol_post3})}   \\
tex \specialcell{Driving causing death} & \multicolumn{1}{c}{${ymean_off_driving_death}} &  \multicolumn{1}{c}{ ${b_off_driving_death_pre}${off_driving_death_pre_star} } &  \multicolumn{1}{c}{(${se_off_driving_death_pre})} &  \multicolumn{1}{c}{ ${b_off_driving_death_post1}${off_driving_death_post1_star} } &  \multicolumn{1}{c}{(${se_off_driving_death_post1})} &  \multicolumn{1}{c}{ ${b_off_driving_death_post2}${off_driving_death_post2_star} } &  \multicolumn{1}{c}{(${se_off_driving_death_post2})} &  \multicolumn{1}{c}{ ${b_off_driving_death_post3}${off_driving_death_post3_star} } &  \multicolumn{1}{c}{(${se_off_driving_death_post3})}   \\
tex \specialcell{Hard drug sale} & \multicolumn{1}{c}{${ymean_off_drug_sale}} &  \multicolumn{1}{c}{ ${b_off_drug_sale_pre}${off_drug_sale_pre_star} } &  \multicolumn{1}{c}{(${se_off_drug_sale_pre})} &  \multicolumn{1}{c}{ ${b_off_drug_sale_post1}${off_drug_sale_post1_star} } &  \multicolumn{1}{c}{(${se_off_drug_sale_post1})} &  \multicolumn{1}{c}{ ${b_off_drug_sale_post2}${off_drug_sale_post2_star} } &  \multicolumn{1}{c}{(${se_off_drug_sale_post2})} &  \multicolumn{1}{c}{ ${b_off_drug_sale_post3}${off_drug_sale_post3_star} } &  \multicolumn{1}{c}{(${se_off_drug_sale_post3})}   \\
tex \specialcell{Opioid drug sale} & \multicolumn{1}{c}{${ymean_off_drug_opiates_sale}} &  \multicolumn{1}{c}{ ${b_off_drug_opiates_sale_pre}${off_drug_opiates_sale_pre_star} } &  \multicolumn{1}{c}{(${se_off_drug_opiates_sale_pre})} &  \multicolumn{1}{c}{ ${b_off_drug_opiates_sale_post1}${off_drug_opiates_sale_post1_star} } &  \multicolumn{1}{c}{(${se_off_drug_opiates_sale_post1})} &  \multicolumn{1}{c}{ ${b_off_drug_opiates_sale_post2}${off_drug_opiates_sale_post2_star} } &  \multicolumn{1}{c}{(${se_off_drug_opiates_sale_post2})} &  \multicolumn{1}{c}{ ${b_off_drug_opiates_sale_post3}${off_drug_opiates_sale_post3_star} } &  \multicolumn{1}{c}{(${se_off_drug_opiates_sale_post3})}   \\
tex \specialcell{Non-opioid drug sale} & \multicolumn{1}{c}{${ymean_off_drug_hardsale}} &  \multicolumn{1}{c}{ ${b_off_drug_hardsale_pre}${off_drug_hardsale_pre_star} } &  \multicolumn{1}{c}{(${se_off_drug_hardsale_pre})} &  \multicolumn{1}{c}{ ${b_off_drug_hardsale_post1}${off_drug_hardsale_post1_star} } &  \multicolumn{1}{c}{(${se_off_drug_hardsale_post1})} &  \multicolumn{1}{c}{ ${b_off_drug_hardsale_post2}${off_drug_hardsale_post2_star} } &  \multicolumn{1}{c}{(${se_off_drug_hardsale_post2})} &  \multicolumn{1}{c}{ ${b_off_drug_hardsale_post3}${off_drug_hardsale_post3_star} } &  \multicolumn{1}{c}{(${se_off_drug_hardsale_post3})}   \\
tex \\
tex & & \multicolumn{8}{c}{\specialcell{C. Other violent }} \\
tex \\
tex \specialcell{Serious assault \\ (with injury)} & \multicolumn{1}{c}{${ymean_off_assault_sinj}} &  \multicolumn{1}{c}{ ${b_off_assault_sinj_pre}${off_assault_sinj_pre_star} } &  \multicolumn{1}{c}{(${se_off_assault_sinj_pre})} &  \multicolumn{1}{c}{ ${b_off_assault_sinj_post1}${off_assault_sinj_post1_star} } &  \multicolumn{1}{c}{(${se_off_assault_sinj_post1})} &  \multicolumn{1}{c}{ ${b_off_assault_sinj_post2}${off_assault_sinj_post2_star} } &  \multicolumn{1}{c}{(${se_off_assault_sinj_post2})} &  \multicolumn{1}{c}{ ${b_off_assault_sinj_post3}${off_assault_sinj_post3_star} } &  \multicolumn{1}{c}{(${se_off_assault_sinj_post3})}   \\
tex \specialcell{Serious assault \;\;\; \\ (with no injury)} & \multicolumn{1}{c}{${ymean_off_assault_sninj}} &  \multicolumn{1}{c}{ ${b_off_assault_sninj_pre}${off_assault_sninj_pre_star} } &  \multicolumn{1}{c}{(${se_off_assault_sninj_pre})} &  \multicolumn{1}{c}{ ${b_off_assault_sninj_post1}${off_assault_sninj_post1_star} } &  \multicolumn{1}{c}{(${se_off_assault_sninj_post1})} &  \multicolumn{1}{c}{ ${b_off_assault_sninj_post2}${off_assault_sninj_post2_star} } &  \multicolumn{1}{c}{(${se_off_assault_sninj_post2})} &  \multicolumn{1}{c}{ ${b_off_assault_sninj_post3}${off_assault_sninj_post3_star} } &  \multicolumn{1}{c}{(${se_off_assault_sninj_post3})}   \\
texdoc close

   
   
   

/*******************************************************************************
********************************************************************************
Table A.3: Robustness to alternative specifications

********************************************************************************
*******************************************************************************/

use "data/panel.dta", clear

gen off_drug_harduse=off_drug_hard_nonO_use

*(1) No controls

*types of mortality
foreach var of varlist mortality mortality_opiates2 off_drug_harduse off_violent_nonrobb  {
capture drop R*
gen R`var'=`var'*1000*4

reg R`var'  treated ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr  ,  vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls " "	
estadd loc indfe " "
estadd loc pretrend " "
estadd loc econvars " "

estadd scalar rsquared=`rsquare'

est store `var'A3B_1
}

foreach var of varlist  off_property_serious {
capture drop R*
gen R`var'=`var'*1000*4

reg R`var'  treated ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr ///
 if (treated==1 | (treated==0  & (pre_off_breakenter>0 | pre_off_mv_theft>0 | pre_off_robbery>0))) ///
 ,  vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls " "	
estadd loc indfe " "
estadd loc pretrend " "
estadd loc econvars " "

estadd scalar rsquared=`rsquare'

est store `var'A3B_1
}


 
*new column 2 - just demographic controls and ind FE no age


*types of mortality
foreach var of varlist mortality mortality_opiates2  {
capture drop R*
gen R`var'=`var'*1000*4

reg R`var' $indcontrols $offcontrols ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr  ,  vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls "  "	
estadd loc indfe " $\checkmark$ "
estadd loc pretrend " "
estadd loc econvars " "

estadd scalar rsquared=`rsquare'

est store `var'A3C_2
}


foreach var of varlist off_drug_harduse off_violent_nonrobb  {

capture drop R*
gen R`var'=`var'*1000*4

*** Diff-in-Diff
areg R`var' $indcontrols $offcontrols ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr , absorb(ppn) vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls "  "	
estadd loc indfe " $\checkmark$ "
estadd loc pretrend " "
estadd loc econvars "  "

estadd scalar rsquared=`rsquare'

est store `var'A3C_2
}


foreach var of varlist  off_property_serious {

capture drop R*
gen R`var'=`var'*1000*4

*** Diff-in-Diff
areg R`var' $indcontrols $offcontrols ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr ///
 if (treated==1 | (treated==0  & (pre_off_breakenter>0 | pre_off_mv_theft>0 | pre_off_robbery>0))) ///
, absorb(ppn) vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls "  "	
estadd loc indfe " $\checkmark$ "
estadd loc pretrend " "
estadd loc econvars "  "

estadd scalar rsquared=`rsquare'

est store `var'A3C_2
}



*(2) add age controls

foreach var of varlist mortality mortality_opiates2  {
capture drop R*
gen R`var'=`var'*1000*4

reg R`var' $indcontrols $agecontrols $offcontrols ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr  ,  vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls " $\checkmark$ "	
estadd loc indfe " "
estadd loc pretrend " "
estadd loc econvars " "

estadd scalar rsquared=`rsquare'

est store `var'A3B_2
}



* (3) individual FE

foreach var of varlist off_drug_harduse off_violent_nonrobb  {

capture drop R*
gen R`var'=`var'*1000*4

*** Diff-in-Diff
areg R`var' $indcontrols $agecontrols $offcontrols ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr , absorb(ppn) vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls " $\checkmark$ "	
estadd loc indfe " $\checkmark$ "
estadd loc pretrend " "
estadd loc econvars "  "

estadd scalar rsquared=`rsquare'

est store `var'A3B_3
}


foreach var of varlist  off_property_serious {

capture drop R*
gen R`var'=`var'*1000*4


*** Diff-in-Diff
areg R`var' $indcontrols $agecontrols $offcontrols ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr ///
 if (treated==1 | (treated==0  & (pre_off_breakenter>0 | pre_off_mv_theft>0 | pre_off_robbery>0))) ///
 , absorb(ppn) vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls " $\checkmark$ "	
estadd loc indfe " $\checkmark$ "
estadd loc pretrend " "
estadd loc econvars "  "

estadd scalar rsquared=`rsquare'

est store `var'A3B_3
}



*(4) Pre-period linear trend

gen trend=yq-yq(1995,4)

global treatvars "trend Ayear_2001 Ayear_2002 Ayear_2003 Ayear_2004 Ayear_2005 Ayear_2006 Ayear_2007 Ayear_2008 Atyear_2001 Atyear_2002 Atyear_2003 Atyear_2004 Atyear_2005 Atyear_2006 Atyear_2007 Atyear_2008"

*types of mortality
foreach var of varlist mortality mortality_opiates2  {
capture drop R*
gen R`var'=`var'*1000*4

reg R`var' $indcontrols $agecontrols $offcontrols ///
$treatvars q4y2000 q4y2000_T i.qtr  ,  vce(cluster ppn)

local rsquare=e(r2)

nlcom (post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls " $\checkmark$ "	
estadd loc indfe " $\checkmark$ "
estadd loc pretrend " $\checkmark$ "
estadd loc econvars "  "


estadd scalar rsquared=`rsquare'

est store `var'A3B_4
}

*crime

foreach var of varlist off_drug_harduse off_violent_nonrobb  {

capture drop R*
gen R`var'=`var'*1000*4

*** Diff-in-Diff
areg R`var' $indcontrols $agecontrols $offcontrols ///
$treatvars q4y2000 q4y2000_T i.qtr , absorb(ppn) vce(cluster ppn)

local rsquare=e(r2)

nlcom (post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls " $\checkmark$ "	
estadd loc indfe " $\checkmark$ "
estadd loc pretrend " $\checkmark$ "
estadd loc econvars "  "

estadd scalar rsquared=`rsquare'

est store `var'A3B_4
}


foreach var of varlist off_property_serious {

capture drop R*
gen R`var'=`var'*1000*4

*** Diff-in-Diff
areg R`var' $indcontrols $agecontrols $offcontrols ///
$treatvars q4y2000 q4y2000_T i.qtr ///
 if (treated==1 | (treated==0  & (pre_off_breakenter>0 | pre_off_mv_theft>0 | pre_off_robbery>0))) ///
 , absorb(ppn) vce(cluster ppn)

local rsquare=e(r2)

nlcom (post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls " $\checkmark$ "	
estadd loc indfe " $\checkmark$ "
estadd loc pretrend " $\checkmark$ "
estadd loc econvars "  "

estadd scalar rsquared=`rsquare'

est store `var'A3B_4
}


 

*(5) Economic controls

preserve
import delimited "data/abs_lfp_data.csv", clear
gen date_mdy=mdy(month,1,year)
gen ym=mofd(date_mdy)
gen yq=qofd(date_mdy)
format ym %tm
format yq %tq
keep if yq>=yq(1995,4) & yq<=yq(2008,4)
collapse (mean) emppop_* urate_*, by(yq)
keep yq emppop_* urate_*
tempfile unemp
save `unemp'
restore

capture drop _merge
merge m:1 yq using `unemp'

foreach var of varlist urate_original  {
gen `var'_T=`var'*treated
}

global econvars "urate_original*  "

*types of mortality
foreach var of varlist mortality mortality_opiates2  {
capture drop R*
gen R`var'=`var'*1000*4

reg R`var' $indcontrols $agecontrols $offcontrols $econvars ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr  ,  vce(cluster ppn)


local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls " $\checkmark$ "	
estadd loc indfe " $\checkmark$ "
estadd loc pretrend "  "
estadd loc econvars " $\checkmark$ "

estadd scalar rsquared=`rsquare'

est store `var'A3B_5
}


*crime

foreach var of varlist off_drug_harduse off_violent_nonrobb  {

capture drop R*
gen R`var'=`var'*1000*4

*** Diff-in-Diff
areg R`var' $indcontrols $agecontrols $offcontrols $econvars ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr , absorb(ppn) vce(cluster ppn)


local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls " $\checkmark$ "	
estadd loc indfe " $\checkmark$ "
estadd loc pretrend "  "
estadd loc econvars " $\checkmark$ "

estadd scalar rsquared=`rsquare'

est store `var'A3B_5
}



foreach var of varlist  off_property_serious {

capture drop R*
gen R`var'=`var'*1000*4

*** Diff-in-Diff
areg R`var' $indcontrols $agecontrols $offcontrols $econvars ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr ///
 if (treated==1 | (treated==0  & (pre_off_breakenter>0 | pre_off_mv_theft>0 | pre_off_robbery>0))) ///
 , absorb(ppn) vce(cluster ppn)


local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls " $\checkmark$ "	
estadd loc indfe " $\checkmark$ "
estadd loc pretrend "  "
estadd loc econvars " $\checkmark$ "

estadd scalar rsquared=`rsquare'

est store `var'A3B_5
}





local titles2 " & \multicolumn{1}{c}{\specialcell{No \\ controls \\ }} &  \multicolumn{1}{c}{\specialcell{Add \\ dem. controls \\ or indiv. FE}} &  \multicolumn{1}{c}{\specialcell{Add age \\ controls}}     & \multicolumn{1}{c}{\specialcell{Include \\ pre-trend }} & \multicolumn{1}{c}{\specialcell{Add controls \\ for unemp. rate}}     \\"
local numbers " \cmidrule(lr){2-6} & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)} & \multicolumn{1}{c}{(4)} & \multicolumn{1}{c}{(5)}   \\ \hline "






local panel " \hline \\ \multicolumn{6}{l}{\textit{\underline{A. Opioid-related mortality:}}} \\ "

 
esttab mortality_opiates2A3B_1 mortality_opiates2A3C_2 mortality_opiates2A3B_2 mortality_opiates2A3B_4  mortality_opiates2A3B_5     ///
using "results/TableA3.tex", replace f margin  ///
keep(pre post1 post2 post3) coeflabels(pre " Years -5 to -2" ///
post1 "Year 0" post2 "Year 1" ///
post3 "Years 2 to 7" )   ///
label booktabs b(4) p(4) alignment(S)  /// 
collabels(none)  ///
mlabels(none) nonumbers  ///
posthead( "`titles2'"  "`numbers'" "`panel'") ///
star(*  0.05 ** 0.01) compress ///
width(\hsize) ///
cells("b(fmt(a2)star )" "se(fmt(a2)par)")  noobs nolines nonum 

local panel " \\ \multicolumn{6}{l}{\underline{\textit{B. All-cause mortality:}}} \\ "

 
esttab mortalityA3B_1 mortalityA3C_2 mortalityA3B_2 mortalityA3B_4  mortalityA3B_5     ///
using "results/TableA3.tex", append f margin  ///
keep(pre post1 post2 post3) coeflabels(pre " Years -5 to -2" ///
post1 "Year 0" post2 "Year 1" ///
post3 "Years 2 to 7" )   ///
label booktabs b(4) p(4) alignment(S)  /// 
collabels(none)  ///
mlabels(none) nonumbers  ///
posthead(   "`panel'") ///
star(*  0.05 ** 0.01) compress ///
width(\hsize) ///
cells("b(fmt(a2)star )" "se(fmt(a2)par)")  noobs nolines nonum  

local panel " \hline \\ \multicolumn{6}{l}{\textit{\underline{C. Non-opioid drug use/possession:}}} \\ "

 
esttab off_drug_harduseA3B_1 off_drug_harduseA3C_2 off_drug_harduseA3B_3 off_drug_harduseA3B_4 off_drug_harduseA3B_5    ///
using "results/TableA3.tex", append f margin  ///
keep(pre post1 post2 post3) coeflabels(pre " Years -5 to -2" ///
post1 "Year 0" post2 "Year 1" ///
post3 "Years 2 to 7" )   ///
label booktabs b(4) p(4) alignment(S)  /// 
collabels(none)  ///
mlabels(none) nonumbers  ///
posthead(  "`panel'") ///
star(*  0.05 ** 0.01) compress ///
width(\hsize) ///
cells("b(fmt(a2)star )" "se(fmt(a2)par)")  noobs nolines nonum 



local panel " \hline \\ \multicolumn{6}{l}{\textit{\underline{D. Violent offenses (excl. robbery):}}} \\  "

 
esttab off_violent_nonrobbA3B_1 off_violent_nonrobbA3C_2  off_violent_nonrobbA3B_3 off_violent_nonrobbA3B_4 off_violent_nonrobbA3B_5    ///
using "results/TableA3.tex", append f margin  ///
keep(pre post1 post2 post3) coeflabels(pre " Years -5 to -2" ///
post1 "Year 0" post2 "Year 1" ///
post3 "Years 2 to 7" )   ///
label booktabs b(4) p(4) alignment(S)  /// 
collabels(none)  ///
mlabels(none) nonumbers  ///
posthead(  "`panel'") ///
star(*  0.05 ** 0.01) compress ///
width(\hsize) ///
cells("b(fmt(a2)star )" "se(fmt(a2)par)")  noobs nolines nonum 




local panel " \hline \\ \multicolumn{6}{l}{\textit{\underline{E. Serious property offenses (robbery, burglary, vehicle theft):}}} \\ "

 
esttab off_property_seriousA3B_1 off_property_seriousA3C_2  off_property_seriousA3B_3 off_property_seriousA3B_4 off_property_seriousA3B_5   ///
using "results/TableA3.tex", append f margin  ///
keep(pre post1 post2 post3) coeflabels(pre " Years -5 to -2" ///
post1 "Year 0" post2 "Year 1" ///
post3 "Years 2 to 7" )   ///
label booktabs b(4) p(4) alignment(S)  /// 
collabels(none)  ///
mlabels(none) nonumbers  ///
posthead(  "`panel'") ///
star(*  0.05 ** 0.01) compress ///
width(\hsize) ///
cells("b(fmt(a2)star )" "se(fmt(a2)par)")  noobs nolines nonum ///
stats( space  indfe controls pretrend econvars  , fmt(%9.0fc %str2 %str2 %str2 %str2  ) ///
layout( "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}"    ) ///
labels( "\hline" "\specialcell{Dem. controls / ind. FE}" "\specialcell{Age controls}" "\specialcell{Pre-reduction trend}" "\specialcell{Unemp. rate controls}"    ))

   
   
   

/*********************************

Table A.4: Robustness to trends in government services

*********************************/


use "data/panel.dta", clear

gen off_drug_harduse=off_drug_hard_nonO_use

*(6) Add controls for police officers, prisoners, mental health service employees, disability social insurance payments 

*gen yq panel to merge in with government spending annual data
preserve
clear 
set obs 14
gen year=1995 if _n==1
replace year=_n+1994 if _n>1
expand 4 if year>=1996
gen quarter=4 if year==1995
sort year
bys year: replace quarter=_n if year>=1996
gen yq=yq(year,quarter)
tempfile yq
save `yq'

import delimited "data/annual_data.csv", clear
gen quarter=4 // financial years, so 1996.5 is July 1996 - June 1997
gen yq=yq(year2,quarter)

gen staff_pc=police_staff/nsw_pop_dec*100000
gen prison_pop=aveprisonpop/nsw_pop_dec*100000
gen prison_capacity=prisdesigncapacity_total/nsw_pop_dec*100000

keep yq staff_pc police_officers prison_pop mental_ftestaff mental_beds police_staff aveprisonpop prisdesigncapacity_total

merge 1:m yq using `yq'
drop _merge

sort yq

foreach var of varlist staff_pc police_staff police_officers prison_pop prisdesigncapacity_total mental_ftestaff mental_beds  aveprisonpop  {
	ipolate `var' yq, gen(`var'_E) epolate
}

keep *_E yq

tempfile a
save `a'

*pension payments
import delimited "data/dss_pensions.csv", clear
gen date=v1
split date, parse("/")
replace date3="2007" if date3=="2007 Note O"
destring date2, gen(month)
destring date3, gen(year)
gen quarter=1 if month==3
replace quarter=2 if month==3
replace quarter=3 if month==9
replace quarter=4 if month==9
gen yq=yq(year,quarter)

gen scaling=(113.2940647/cpi_notax)
gen disability_SI = disability_single*scaling
gen unemp_SI = unemployment*scaling

keep disability_SI unemp_SI yq

merge 1:1 yq using `yq'

keep yq disability_SI unemp_SI

sort yq

foreach var of varlist *_SI {
	ipolate `var' yq, gen(`var'_E) epolate
}

keep yq *_E

merge 1:1 yq using `a'
keep if _merge==3
drop _merge

tempfile b
save `b'

restore 

capture drop _merge
merge m:1 yq using `b'
drop _merge

foreach var of varlist *_E  {
gen `var'_T=`var'*treated
}


*global policyvars " staff_pcE* prison_popE* mental_ftestaffE* mental_bedsE* disability_SI* unemp_SI* "
global policyvars0 "treated "
global policyvars1 "police_officers_E*"
global policyvars2 "prison_pop_E* "
global policyvars3 "mental_ftestaff_E* "
global policyvars4 "disability_SI_E* "


global indcontrols "treated i.female i.indig i.female_missing i.indig_missing  "
global agecontrols "i.age_int  i.age_int#i.female i.age_int#i.indig "
global offcontrols "i.first_drug_off_ym"

forvalues p=0/4 {

*types of mortality
foreach var of varlist mortality mortality_opiates2  {
capture drop R*
gen R`var'=`var'*1000*4


reg R`var' i.female i.indig i.female_missing i.indig_missing $agecontrols $offcontrols ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr  ${policyvars`p'},  vce(cluster ppn)


local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls " $\checkmark$ "	
estadd loc indfe " $\checkmark$ "
estadd loc pretrend "  "
estadd loc econvars " $\checkmark$ "

estadd scalar rsquared=`rsquare'

est store `var'A3B_6_`p'
}


*crime

foreach var of varlist off_drug_harduse off_violent_nonrobb  {

capture drop R*
gen R`var'=`var'*1000*4

*** Diff-in-Diff
reg R`var' i.female i.indig i.female_missing i.indig_missing $agecontrols $offcontrols  ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr ${policyvars`p'},   vce(cluster ppn)


local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls " $\checkmark$ "	
estadd loc indfe " $\checkmark$ "
estadd loc pretrend "  "
estadd loc econvars " $\checkmark$ "

estadd scalar rsquared=`rsquare'

est store `var'A3B_6_`p'
}


foreach var of varlist  off_property_serious {

capture drop R*
gen R`var'=`var'*1000*4

*** Diff-in-Diff
reg R`var' i.female i.indig i.female_missing i.indig_missing $agecontrols $offcontrols  ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr ${policyvars`p'} ///
 if (treated==1 | (treated==0  & (pre_off_breakenter>0 | pre_off_mv_theft>0 | pre_off_robbery>0))) ///
 ,   vce(cluster ppn)


local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls " $\checkmark$ "	
estadd loc indfe " $\checkmark$ "
estadd loc pretrend "  "
estadd loc econvars " $\checkmark$ "

estadd scalar rsquared=`rsquare'

est store `var'A3B_6_`p'
}

}



 
*unique ppn

*** Create identifiers for meth and cocaine offenses in pre-period
preserve
keep if yq <= yq(2000,4)

*unique ppn

keep ppn first_off_drug_amph_use off_drug_amph_use first_off_drug_cocaine_use off_drug_cocaine_use

bys ppn: egen meth_counts1=sum(first_off_drug_amph_use)
bys ppn: egen meth_counts2=sum(off_drug_amph_use)

bys ppn: egen cocaine_counts1=sum(first_off_drug_cocaine_use)
bys ppn: egen cocaine_counts2=sum(off_drug_cocaine_use)

gen meth_user=0
replace meth_user=1 if meth_counts1~=0
replace meth_user=1 if meth_counts2~=0

gen cocaine_user=0
replace cocaine_user=1 if cocaine_counts1~=0
replace cocaine_user=1 if cocaine_counts2~=0

keep ppn meth_user cocaine_user

bysort ppn: keep if _n==1

tab meth_user 
tab cocaine_user

tempfile othdrugs
save `othdrugs'
restore 

sort ppn
merge m:1 ppn using `othdrugs'
replace cocaine_user=0 if _merge==1
replace meth_user=0 if _merge==1
drop _merge


*** Add illicit drug prices
*gen yq panel to merge in with government spending annual data
preserve
clear 
set obs 14
gen year=1995 if _n==1
replace year=_n+1994 if _n>1
expand 4 if year>=1996
gen quarter=4 if year==1995
sort year
bys year: replace quarter=_n if year>=1996
gen yq=yq(year,quarter)
tempfile yq
save `yq'

insheet using "data/price_heroin_cocaine_meth_yq.csv", clear
keep year qtr coc_price_ref meth_price_ref

rename coc_price_ref coc_price1
rename meth_price_ref meth_price1
  
keep coc_price1 meth_price1 year qtr
gen yq=yq(year,qtr)
drop year qtr

merge 1:1 yq using `yq'
drop _merge

*extrapolate back in time for pre 1998 period
foreach var of varlist coc_price1 meth_price1 {
	ipolate `var' yq, gen(`var'_E) epolate
}

*recode price to zero if extrapolation makes negative 
replace coc_price1_E  = 0 if coc_price1_E<0
replace meth_price1_E = 0 if meth_price1_E<0

keep *_E yq


sort yq

tempfile prices
save `prices'

restore

sort yq
merge m:1 yq using `prices'
drop _merge


*** Outcomes - drug use - controls based on drug offenses for people previsouly arrested for the drugs

gen coc_price1  = coc_price1_E *cocaine_user
gen coc_price2  = coc_price1_E *(1 - cocaine_user)
gen meth_price1 = meth_price1_E*meth_user
gen meth_price2 = meth_price1_E*(1 - meth_user) 

global policyvars5 "coc_price1 coc_price2 meth_price1 meth_price2 "

forvalues p=5/5 {

*types of mortality
foreach var of varlist mortality mortality_opiates2  {
capture drop R*
gen R`var'=`var'*1000*4

global Ayears "Atyear_1998 Atyear_1999 Atyear_2001 Atyear_2002 Atyear_2003 Atyear_2004 Atyear_2005 Atyear_2006 Atyear_2007 Atyear_2008 Ayear_1998 Ayear_1999 Ayear_2001 Ayear_2002 Ayear_2003 Ayear_2004 Ayear_2005 Ayear_2006 Ayear_2007 Ayear_2008"

reg R`var' $indcontrols $agecontrols $offcontrols ///
$Ayears q4y2000 q4y2000_T i.qtr  ${policyvars`p'},  vce(cluster ppn)


local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1998]+_b[Atyear_1999])/2)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls " $\checkmark$ "	
estadd loc indfe " $\checkmark$ "
estadd loc pretrend "  "
estadd loc econvars " $\checkmark$ "

estadd scalar rsquared=`rsquare'

est store `var'A3B_6_`p'
}


*crime

foreach var of varlist off_drug_harduse off_violent_nonrobb  {

capture drop R*
gen R`var'=`var'*1000*4

*** Diff-in-Diff
reg R`var' $indcontrols $agecontrols $offcontrols  ///
$Ayears q4y2000 q4y2000_T i.qtr ${policyvars`p'},   vce(cluster ppn)


local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1998]+_b[Atyear_1999])/2)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls " $\checkmark$ "	
estadd loc indfe " $\checkmark$ "
estadd loc pretrend "  "
estadd loc econvars " $\checkmark$ "

estadd scalar rsquared=`rsquare'

est store `var'A3B_6_`p'
}


foreach var of varlist off_property_serious {

capture drop R*
gen R`var'=`var'*1000*4

*** Diff-in-Diff
reg R`var' $indcontrols $agecontrols $offcontrols  ///
$Ayears q4y2000 q4y2000_T i.qtr ${policyvars`p'} ///
 if (treated==1 | (treated==0  & (pre_off_breakenter>0 | pre_off_mv_theft>0 | pre_off_robbery>0))) ///
 ,   vce(cluster ppn)


local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1998]+_b[Atyear_1999])/2)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc controls " $\checkmark$ "	
estadd loc indfe " $\checkmark$ "
estadd loc pretrend "  "
estadd loc econvars " $\checkmark$ "

estadd scalar rsquared=`rsquare'

est store `var'A3B_6_`p'
}



}


local titles2 " & \multicolumn{1}{c}{\specialcell{Baseline \\ results \\ \; \\ \; }} & \multicolumn{1}{c}{\specialcell{Control for \\ police \\ staffing \\ trends }} &  \multicolumn{1}{c}{\specialcell{Control \\ for \\ incarceration \\ trends}} &  \multicolumn{1}{c}{\specialcell{Control for \\ mental health \\ staff \\ trends}}     & \multicolumn{1}{c}{\specialcell{Control \\ for \\ disability \\ trends }}  & \multicolumn{1}{c}{\specialcell{Control for \\ meth \\ \& cocaine \\ prices}}    \\"
local numbers " \cmidrule(lr){2-7} & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)} & \multicolumn{1}{c}{(4)} & \multicolumn{1}{c}{(5)} & \multicolumn{1}{c}{(6)}  \\ \hline "

local panel " \hline \\ \multicolumn{7}{l}{\textit{\underline{A. Opioid-related mortality:}}} \\ "

 
esttab  mortality_opiates2A3B_6_0 mortality_opiates2A3B_6_1 mortality_opiates2A3B_6_2 mortality_opiates2A3B_6_3 mortality_opiates2A3B_6_4 mortality_opiates2A3B_6_5     ///
using "results/TableA4.tex", replace f margin  ///
keep(pre post1 post2 post3) coeflabels(pre " Years -5 to -2" ///
post1 "Year 0" post2 "Year 1" ///
post3 "Years 2 to 7" )   ///
label booktabs b(4) p(4) alignment(S)  /// 
collabels(none)  ///
mlabels(none) nonumbers  ///
posthead(  "`titles2'"  "`numbers'" "`panel'" ) ///
star(*  0.05 ** 0.01) compress ///
width(\hsize) ///
cells("b(fmt(a2)star )" "se(fmt(a2)par)")  noobs nolines nonum 


local panel " \\ \multicolumn{7}{l}{\underline{\textit{B. All-cause mortality:}}} \\ "

 
esttab  mortalityA3B_6_0 mortalityA3B_6_1 mortalityA3B_6_2 mortalityA3B_6_3 mortalityA3B_6_4 mortalityA3B_6_5      ///
using "results/TableA4.tex", append f margin  ///
keep(pre post1 post2 post3) coeflabels(pre " Years -5 to -2" ///
post1 "Year 0" post2 "Year 1" ///
post3 "Years 2 to 7" )   ///
label booktabs b(4) p(4) alignment(S)  /// 
collabels(none)  ///
mlabels(none) nonumbers  ///
posthead( "`panel'") ///
star(*  0.05 ** 0.01) compress ///
width(\hsize) ///
cells("b(fmt(a2)star )" "se(fmt(a2)par)")  noobs nolines nonum  





local panel " \hline \\ \multicolumn{7}{l}{\textit{\underline{C. Non-opioid drug use/possession:}}} \\ "

 
esttab  off_drug_harduseA3B_6_0 off_drug_harduseA3B_6_1 off_drug_harduseA3B_6_2 off_drug_harduseA3B_6_3 off_drug_harduseA3B_6_4 off_drug_harduseA3B_6_5    ///
using "results/TableA4.tex", append f margin  ///
keep(pre post1 post2 post3) coeflabels(pre " Years -5 to -2" ///
post1 "Year 0" post2 "Year 1" ///
post3 "Years 2 to 7" )   ///
label booktabs b(4) p(4) alignment(S)  /// 
collabels(none)  ///
mlabels(none) nonumbers  ///
posthead(  "`panel'") ///
star(*  0.05 ** 0.01) compress ///
width(\hsize) ///
cells("b(fmt(a2)star )" "se(fmt(a2)par)")  noobs nolines nonum 



local panel " \hline \\ \multicolumn{7}{l}{\textit{\underline{D. Violent offenses (excl. robbery):}}} \\  "

 
esttab  off_violent_nonrobbA3B_6_0 off_violent_nonrobbA3B_6_1 off_violent_nonrobbA3B_6_2 off_violent_nonrobbA3B_6_3 off_violent_nonrobbA3B_6_4 off_violent_nonrobbA3B_6_5    ///
using "results/TableA4.tex", append f margin  ///
keep(pre post1 post2 post3) coeflabels(pre " Years -5 to -2" ///
post1 "Year 0" post2 "Year 1" ///
post3 "Years 2 to 7" )   ///
label booktabs b(4) p(4) alignment(S)  /// 
collabels(none)  ///
mlabels(none) nonumbers  ///
posthead(  "`panel'") ///
star(*  0.05 ** 0.01) compress ///
width(\hsize) ///
cells("b(fmt(a2)star )" "se(fmt(a2)par)")  noobs nolines nonum 




local panel " \hline \\ \multicolumn{7}{l}{\textit{\underline{E. Serious property offenses (robbery, burglary, vehicle theft):}}} \\ "

 
esttab off_property_seriousA3B_6_0 off_property_seriousA3B_6_1 off_property_seriousA3B_6_2 off_property_seriousA3B_6_3 off_property_seriousA3B_6_4 off_property_seriousA3B_6_5    ///
using "results/TableA4.tex", append f margin  ///
keep(pre post1 post2 post3) coeflabels(pre " Years -5 to -2" ///
post1 "Year 0" post2 "Year 1" ///
post3 "Years 2 to 7" )   ///
label booktabs b(4) p(4) alignment(S)  /// 
collabels(none)  ///
mlabels(none) nonumbers  ///
posthead(  "`panel'") ///
star(*  0.05 ** 0.01) compress ///
width(\hsize) ///
cells("b(fmt(a2)star )" "se(fmt(a2)par)")  noobs nolines nonum    







/*******************************************************************************
********************************************************************************
Table A.5: Robustness to alternative sample definitions for
comparison group
********************************************************************************
*******************************************************************************/


use "data/panel.dta", clear

gen off_drug_harduse=off_drug_hard_nonO_use


*types of mortality
foreach var of varlist mortality mortality_opiates2  {
capture drop R*
gen R`var'=`var'*1000*4

reg R`var' $indcontrols $agecontrols $offcontrols ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr  ,  vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc baseline ""	
estadd loc dd "$\checkmark$"

estadd scalar rsquared=`rsquare'

preserve
keep if e(sample)==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_ind =r(N)
restore

preserve
keep if e(sample)==1 & treated==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_indT =r(N)
restore

preserve
keep if e(sample)==1 & treated==0
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_indC =r(N)
restore

est store `var'A4_1
}


*crime

foreach var of varlist off_drug_harduse off_violent_nonrobb off_property_serious off_theft_nonmv {

capture drop R*
gen R`var'=`var'*1000*4

*** Diff-in-Diff
areg R`var' $indcontrols $agecontrols $offcontrols ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr , absorb(ppn) vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc baseline ""	
estadd loc dd "$\checkmark$"

estadd scalar rsquared=`rsquare'

preserve
keep if e(sample)==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_ind =r(N)
restore


preserve
keep if e(sample)==1 & treated==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_indT =r(N)
restore

preserve
keep if e(sample)==1 & treated==0
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_indC =r(N)
restore

est store `var'A4_1
}


*******************************
*** Alternative property group 
*******************************


*types of mortality
foreach var of varlist mortality mortality_opiates2  {
capture drop R*
gen R`var'=`var'*1000*4

reg R`var' $indcontrols $agecontrols $offcontrols ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr  ///
if (treated==1 | (treated==0  & (pre_off_breakenter>0 | pre_off_mv_theft>0 | pre_off_robbery>0))) ///
,  vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc baseline ""	
estadd loc dd "$\checkmark$"

estadd scalar rsquared=`rsquare'

preserve
keep if e(sample)==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_ind =r(N)
restore

preserve
keep if e(sample)==1 & treated==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_indT =r(N)
restore

preserve
keep if e(sample)==1 & treated==0
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_indC =r(N)
restore

est store `var'A4_2
}


*crime

foreach var of varlist off_drug_harduse off_violent_nonrobb off_property_serious off_theft_nonmv {

capture drop R*
gen R`var'=`var'*1000*4

*** Diff-in-Diff
areg R`var' $indcontrols $agecontrols $offcontrols ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr ///
if (treated==1 | (treated==0  & (pre_off_breakenter>0 | pre_off_mv_theft>0 | pre_off_robbery>0))) ///
 , absorb(ppn) vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc baseline ""	
estadd loc dd "$\checkmark$"

estadd scalar rsquared=`rsquare'

preserve
keep if e(sample)==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_ind =r(N)
restore


preserve
keep if e(sample)==1 & treated==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_indT =r(N)
restore

preserve
keep if e(sample)==1 & treated==0
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_indC =r(N)
restore

est store `var'A4_2
}

************************
** NO COMPARISON GROUP
************************


*types of mortality
foreach var of varlist mortality mortality_opiates2  {
capture drop R*
gen R`var'=`var'*1000*4

reg R`var' $indcontrols $agecontrols $offcontrols ///
Atyear_*  q4y2000_T i.qtr if treated==1 ,  vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc baseline ""	
estadd loc dd "$\checkmark$"

estadd scalar rsquared=`rsquare'

preserve
keep if e(sample)==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_ind =r(N)
restore


preserve
keep if e(sample)==1 & treated==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_indT =r(N)
restore

estadd scalar N_indC =0

est store `var'A4_NC
}


*crime

foreach var of varlist off_drug_harduse off_violent_nonrobb off_property_serious off_theft_nonmv {

capture drop R*
gen R`var'=`var'*1000*4

*** Diff-in-Diff
areg R`var' $indcontrolsB $agecontrols $offcontrols ///
Atyear_* q4y2000_T i.qtr if treated==1, absorb(ppn) vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc baseline ""	
estadd loc dd "$\checkmark$"

estadd scalar rsquared=`rsquare'

preserve
keep if e(sample)==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_ind =r(N)
restore


preserve
keep if e(sample)==1 & treated==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_indT =r(N)
restore

estadd scalar N_indC =0

est store `var'A4_NC
}


************************
** NO COMPARISON GROUP W/ PRE TREND
************************



gen trend=yq-yq(1995,4)
global treatvars "trend  Atyear_2001 Atyear_2002 Atyear_2003 Atyear_2004 Atyear_2005 Atyear_2006 Atyear_2007 Atyear_2008"

*(1) Baseline

*types of mortality
foreach var of varlist mortality mortality_opiates2  {
capture drop R*
gen R`var'=`var'*1000*4

reg R`var' $indcontrols $agecontrols $offcontrols ///
$treatvars q4y2000_T i.qtr if treated==1 ,  vce(cluster ppn)

local rsquare=e(r2)

nlcom (post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc baseline ""	
estadd loc dd "$\checkmark$"

estadd scalar rsquared=`rsquare'

preserve
keep if e(sample)==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_ind =r(N)
restore


preserve
keep if e(sample)==1 & treated==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_indT =r(N)
restore

estadd scalar N_indC =0

est store `var'A4_NC2
}


*crime

foreach var of varlist off_drug_harduse off_violent_nonrobb off_property_serious off_theft_nonmv {

capture drop R*
gen R`var'=`var'*1000*4

*** Diff-in-Diff
areg R`var' $indcontrolsB $agecontrols $treatvars ///
 q4y2000_T i.qtr if treated==1, absorb(ppn) vce(cluster ppn)

local rsquare=e(r2)

nlcom (post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc baseline ""	
estadd loc dd "$\checkmark$"

estadd scalar rsquared=`rsquare'

preserve
keep if e(sample)==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_ind =r(N)
restore


preserve
keep if e(sample)==1 & treated==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_indT =r(N)
restore

estadd scalar N_indC =0

est store `var'A4_NC2
}

*****************
*** Include property and violent offenders in control group
*****************


use "data/panel.dta", clear
keep if treated==1 
keep ppn 
duplicates drop

merge 1:1 ppn using "data/demographic.dta"
gen treated=_merge~=2
drop _merge

preserve 
use "data/phdas_ym.dta", clear
keep ppn
duplicates drop
tempfile ppn
save `ppn'
restore

merge 1:1 ppn using `ppn'
drop if _merge==2

drop if _merge==3 & treated==0

drop _merge


preserve 
use  "data/offense_ym.dta", clear
keep if off_drug_opiates>0 & off_drug_opiates~=. & ym<=ym(2000,12)
keep ppn 
duplicates drop
tempfile opoff
save `opoff'
restore

merge 1:1 ppn using `opoff'
drop if _merge==2
drop if _merge==3 & treated==0
drop _merge



gen female=d_sex==2
gen female_missing=d_sex==.
gen indig=d_indig_bocsar==1
gen indig_missing=d_indig_bocsar==.



preserve 
use  "data/offense_ym.dta", clear
sort ppn ym
bys ppn: egen off_first_ym=min(ym)
keep ppn off_first_ym
duplicates drop
tempfile off
save `off'
restore

merge 1:1 ppn using `off'
drop if _merge==2
drop _merge

drop if off_first_ym>ym(2000,9) & treated==0


preserve 
use  "data/offense_ym.dta", clear
drop if ym>ym(2000,9)
gen off_propvioldrug = off_property + off_violent + off_drug
keep if off_propvioldrug>0

sort ppn ym
gen ym_pvd_off=ym if off_propvioldrug>0 & off_propvioldrug~=.
bys ppn: egen off_first_pvd_ym = min(ym_pvd_off)
drop ym_pvd_off

keep ppn off_first_pvd_ym
duplicates drop
tempfile pvd
save `pvd'
restore


merge m:1 ppn using `pvd'
keep if _merge==3 & treated==0
drop _merge

keep ppn off_first_pvd_ym 

save "data/controlgroup_temp.dta", replace


use "data/mortality_ym.dta", clear
gen mortality_ym=ym
keep ppn mortality_ym
duplicates drop
tempfile mort
save `mort'

use "data/controlgroup_temp.dta", clear
merge 1:1 ppn using `mort'
drop if _merge==2
drop _merge


gen ym_beg=off_first_pvd_ym + 1

gen ym_end=mortality_ym if mortality_ym~=.
replace ym_end=ym(2008,12) if (ym_end==. | ym_end>ym(2008,12))

keep ppn ym_beg ym_end
tempfile begend
save `begend', replace

reshape long ym_, i(ppn) j(j) string
rename ym_ ym
bys ppn: drop if ym==ym[_n-1] & j=="end"
xtset ppn ym

tsfill
drop j

merge 1:1 ppn ym using "data/phdas_ym.dta"
drop if _merge==2
drop _merge
merge 1:1 ppn ym using "data/offense_ym.dta"
drop if _merge==2
drop _merge
merge 1:1 ppn ym using "data/mortality_ym.dta"
drop if _merge==2
drop _merge
merge m:1 ppn using "data/demographic.dta"
drop if _merge==2
drop _merge



foreach var of varlist mortality mortality_overdose* mortality_opiates* mortality_heroin mortality_drugs* mortality_cancer mortality_heart mortality_external mortality_accidental_drug mortality_accidental_injury mortality_suicide mortality_violence off_* phdas_beg_ym phdas_new_ym phdas_end_ym phdas_end_ym_earlydropout phdas_beg_ym_days_on phdas_days_ym   {
replace `var'=0 if `var'==.
}

keep if inrange(ym,ym(1994,1),ym(2008,12))

drop mortality_ucod* mortality_sa2_2011

gen yq=yq(year(dofm(ym)),quarter(dofm(ym)))


gen age=trunc((ym-ym(d_birthyear,d_birthmonth))/12)
replace age=year(dofm(ym))-d_birthyear if d_birthyear!=. & age==.

gen female=d_sex==2
gen female_missing=d_sex==.
gen indig=d_indig_bocsar==1
gen indig_missing=d_indig_bocsar==.

capture ssc install gtools

merge m:1 ppn using `begend'

gcollapse (sum) off_* phdas_days_yq=phdas_days_ym  (first) ym ym_beg  (max)  phdas_new_yq=phdas_new_ym phdas_end_yq=phdas_end_ym phdas_beg_yq=phdas_beg_ym phdas_end_yq_earlydropout=phdas_end_ym_earlydropout mortality* age  female female_missing indig indig_missing  , by(ppn yq)



foreach var of varlist off_* {
gen `var'_ext=(`var'>0 & `var'~=.)
}

gen phdas_yq=(phdas_days_yq>0 & phdas_days_yq~=.)

*new entry into treatment (not in treatment prior quarter)
sort ppn yq
bys ppn: gen phdas_new2_yq=(phdas_yq==1 & phdas_yq[_n-1]==0)


gen year=year(dofq(yq))

gen yq_beg=yq(year(dofm(ym_beg)), quarter(dofm(ym_beg)))

drop mortality_overdose1 mortality_overdose2 mortality_heroin mortality_opiates1 ///
 mortality_opiates2 mortality_opiates3 mortality_drugs mortality_drugsalc ///
 mortality_accidental_drug

sort ppn mortality

merge m:1 ppn mortality using "data/icd.dta"
drop if _merge==2

keep if year>1994
keep if year<2009

replace mortality_opiates=0 if mortality_opiates==. 
replace mortality_opiates2=0 if mortality_opiates2==. 
replace mortality_drugs=0 if mortality_drugs==. 
replace mortality_drugs2=0 if mortality_drugs2==. 
replace mortality_drugsalc=0 if mortality_drugsalc==. 
replace mortality_drugsalc2=0 if mortality_drugsalc2==. 


gen mortality_otherdrugs=mortality_drugs
replace mortality_otherdrugs=0 if mortality_opiates==1
gen mortality_nondrugs=mortality
replace mortality_nondrugs=0 if mortality_drugs==1

gen mortality_otherdrugsalc=mortality_drugsalc
replace mortality_otherdrugsalc=0 if mortality_opiates==1
gen mortality_nondrugsalc=mortality
replace mortality_nondrugsalc=0 if mortality_drugsalc==1

gen mortality_otherdrugs2=mortality_drugs2
replace mortality_otherdrugs2=0 if mortality_opiates2==1
gen mortality_nondrugs2=mortality
replace mortality_nondrugs2=0 if mortality_drugs2==1

gen mortality_otherdrugsalc2=mortality_drugsalc2
replace mortality_otherdrugsalc2=0 if mortality_opiates2==1
gen mortality_nondrugsalc2=mortality
replace mortality_nondrugsalc2=0 if mortality_drugsalc2==1

keep if inrange(yq,yq(1994,4),yq(2008,4))

*keep track of years from Q3 through Q4 prior to shortage
gen Ayear=.
forvalues q=1995/2000 {
replace Ayear=`q' if ((year==`q' & 	inrange(quarter(dofq(yq)),1,3)) | (year==`q'-1 & quarter(dofq(yq))==4))
}

replace Ayear=2000 if yq==yq(2000,4)

forvalues q=2001/2008 {
replace Ayear=`q' if year==`q'
}
 
gen Atyear=.
gen treated=0
replace Atyear=Ayear if treated==1

forvalues q=1995/2008 {
gen Ayear_`q'=(Ayear==`q')
gen Atyear_`q'=(Atyear==`q')
} 

gen q4y2000=yq==yq(2000,4)
gen q4y2000_T=(yq==yq(2000,4) & treated==1)

gen year_beg=year(dofm(ym_beg)) if treated==0
gen age_int=floor(age)  if treated==0

gen qtr=(1960+yq/4-year)*4
tab qtr


*non-mv theft
gen off_theft_nonmv=off_theft - off_mv_theft
summ off_theft_nonmv

*non-robbery violent
gen off_violent_nonrobb=off_violent - off_robbery
summ off_violent_nonrobb

*property including robbery
gen off_property_robb=off_property + off_robbery
summ off_property_robb

*total assault
gen off_assault_total=off_assault + off_sexassault
summ off_assault_total

*remainder for violent
gen off_violent_remain=off_violent - off_robbery - off_assault - off_sexassault - off_murder_plus

save "data/panel_broadcontrol.dta", replace 
erase "data/controlgroup_temp.dta"



use "data/panel.dta", clear

gen off_drug_harduse=off_drug_hard_nonO_use

gen ym_beg=first_drug_off_ym 

append using "data/panel_broadcontrol.dta"

tab treated

drop Ayear_2000 Atyear_2000
keep if inrange(Ayear,1996,2008)

replace off_drug_harduse=off_drug_amph_use+off_drug_cocaine_use+off_drug_other_use if off_drug_harduse==.
replace off_property_serious=off_robbery+off_breakenter+off_mv_theft

*types of mortality
foreach var of varlist mortality mortality_opiates2  {
capture drop R*
gen R`var'=`var'*1000*4

reg R`var' $indcontrols $agecontrols i.ym_beg  ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr  ,  vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc baseline ""	
estadd loc dd "$\checkmark$"

estadd scalar rsquared=`rsquare'

preserve
keep if e(sample)==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_ind =r(N)
restore

preserve
keep if e(sample)==1 & treated==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_indT =r(N)
restore

preserve
keep if e(sample)==1 & treated==0
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_indC =r(N)
restore

est store `var'A5_2
}


*crime

foreach var of varlist off_drug_harduse off_violent_nonrobb off_property_serious off_theft_nonmv {

capture drop R*
gen R`var'=`var'*1000*4

*** Diff-in-Diff
areg R`var' $indcontrols $agecontrols i.ym_beg ///
Atyear_* Ayear_* q4y2000 q4y2000_T i.qtr , absorb(ppn) vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
, post 

preserve
keep if e(sample)==1
sum `var'
estadd scalar N_obs =r(N)
sum `var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1
estadd scalar mean2000=r(mean)*1000*4
restore

estadd loc baseline ""	
estadd loc dd "$\checkmark$"

estadd scalar rsquared=`rsquare'

preserve
keep if e(sample)==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_ind =r(N)
restore


preserve
keep if e(sample)==1 & treated==1
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_indT =r(N)
restore

preserve
keep if e(sample)==1 & treated==0
keep ppn 
duplicates drop 
sum ppn
estadd scalar N_indC =r(N)
restore

est store `var'A5_2
}




local titles2 "\multicolumn{1}{c}{\specialcell{Comparison group: \\ \;}}  & \multicolumn{1}{c}{\specialcell{Non-opioid \\ drug offenders}} & \multicolumn{1}{c}{\specialcell{Non-opioid \\ drug \& prop. \\ offenders }}  & \multicolumn{1}{c}{\specialcell{No \\ comparison \\ group}} & \multicolumn{1}{c}{\specialcell{No \\ comparison \\ group \\ w/ pre trend}} & \multicolumn{1}{c}{\specialcell{Comparison \\ group: \\ Non-opioid drug \\ property \\ and violent \\ offenders}}    \\"
local numbers " \cmidrule(lr){2-6} & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)}  & \multicolumn{1}{c}{(4)}  & \multicolumn{1}{c}{(5)}   \\ \hline "


local panel " \hline \multicolumn{6}{l}{\textit{\underline{A. Opioid-related mortality:}}} \\ "

esttab mortality_opiates2A4_1 mortality_opiates2A4_2 mortality_opiates2A4_NC mortality_opiates2A4_NC2 mortality_opiates2A5_2      ///
using "results/TableA5.tex", replace f margin  ///
keep(pre post1 post2 post3) coeflabels(pre " Years -5 to -2" ///
post1 "Year 0" post2 "Year 1" ///
post3 "Years 2 to 7" )   ///
label booktabs b(4) p(4) alignment(S)  /// 
collabels(none)  ///
mlabels(none) nonumbers  ///
posthead( "`titles2'" "`numbers'" "`panel'") ///
star(*  0.05 ** 0.01) compress ///
width(\hsize) ///
cells("b(fmt(a2)star )" "se(fmt(a2)par)")  noobs nolines nonum ///
stats(  mean2000   , fmt(%9.2fc    ) ///
layout( "\multicolumn{1}{c}{@}"    ) ///
labels( "\hline \specialcell{Treatment group \\ avg. in Year -1  }"      ))


local panel "  \multicolumn{6}{l}{\underline{\textit{B. All-cause mortality:}}} \\ "
 
esttab mortalityA4_1 mortalityA4_2 mortalityA4_NC mortalityA4_NC2 mortalityA5_2      ///
using "results/TableA5.tex", append f margin  ///
keep(pre post1 post2 post3) coeflabels(pre " Years -5 to -2" ///
post1 "Year 0" post2 "Year 1" ///
post3 "Years 2 to 7" )   ///
label booktabs b(4) p(4) alignment(S)  /// 
collabels(none)  ///
mlabels(none) nonumbers  ///
posthead(   "`panel'") ///
star(*  0.05 ** 0.01) compress ///
width(\hsize) ///
cells("b(fmt(a2)star )" "se(fmt(a2)par)")  noobs nolines nonum  ///
stats(  mean2000   , fmt(%9.2fc    ) ///
layout( "\multicolumn{1}{c}{@}"    ) ///
labels( "\hline \specialcell{Treatment group \\ avg. in Year -1  }"      )) 

local panel " \hline \multicolumn{6}{l}{\textit{\underline{C. Non-opioid drug use/possession:}}} \\ "

esttab off_drug_harduseA4_1 off_drug_harduseA4_2 off_drug_harduseA4_NC off_drug_harduseA4_NC2 off_drug_harduseA5_2      ///
using "results/TableA5.tex", append f margin  ///
keep(pre post1 post2 post3) coeflabels(pre " Years -5 to -2" ///
post1 "Year 0" post2 "Year 1" ///
post3 "Years 2 to 7" )   ///
label booktabs b(4) p(4) alignment(S)  /// 
collabels(none)  ///
mlabels(none) nonumbers  ///
posthead(  "`panel'") ///
star(*  0.05 ** 0.01) compress ///
width(\hsize) ///
cells("b(fmt(a2)star )" "se(fmt(a2)par)")  noobs nolines nonum ///
stats(  mean2000   , fmt(%9.2fc    ) ///
layout( "\multicolumn{1}{c}{@}"    ) ///
labels( "\hline \specialcell{Treatment group \\ avg. in Year -1  }"      ))


local panel " \hline \multicolumn{6}{l}{\textit{\underline{D. Violent offenses:}}} \\  "

esttab off_violent_nonrobbA4_1 off_violent_nonrobbA4_2 off_violent_nonrobbA4_NC off_violent_nonrobbA4_NC2 off_violent_nonrobbA5_2    ///
using "results/TableA5.tex", append f margin  ///
keep(pre post1 post2 post3) coeflabels(pre " Years -5 to -2" ///
post1 "Year 0" post2 "Year 1" ///
post3 "Years 2 to 7" )   ///
label booktabs b(4) p(4) alignment(S)  /// 
collabels(none)  ///
mlabels(none) nonumbers  ///
posthead(  "`panel'") ///
star(*  0.05 ** 0.01) compress ///
width(\hsize) ///
cells("b(fmt(a2)star )" "se(fmt(a2)par)")  noobs nolines nonum ///
stats(  mean2000   , fmt(%9.2fc    ) ///
layout( "\multicolumn{1}{c}{@}"    ) ///
labels( "\hline \specialcell{Treatment group \\ avg. in Year -1  }"      ))


local panel " \hline \multicolumn{6}{l}{\textit{\underline{E. Serious Property offenses:}}} \\ "
 
esttab off_property_seriousA4_1 off_property_seriousA4_2 off_property_seriousA4_NC off_property_seriousA4_NC2 off_property_seriousA5_2   ///
using "results/TableA5.tex", append f margin  ///
keep(pre post1 post2 post3) coeflabels(pre " Years -5 to -2" ///
post1 "Year 0" post2 "Year 1" ///
post3 "Years 2 to 7" )   ///
label booktabs b(4) p(4) alignment(S)  /// 
collabels(none)  ///
mlabels(none) nonumbers  ///
posthead(  "`panel'") ///
star(*  0.05 ** 0.01) compress ///
width(\hsize) ///
cells("b(fmt(a2)star )" "se(fmt(a2)par)")  noobs nolines nonum ///
stats( mean2000 space N_indT N_indC  , fmt(%9.2fc %9.0fc %9.0fc %9.0fc   ) ///
layout( "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}"    ) ///
labels( "\hline \specialcell{Treatment group \\ avg. in Year -1  }" ///
 "\hline" "\specialcell{No. of opioid users:}" "\specialcell{No. in control group:}"    ))
 
 
/*******************************************************************************
********************************************************************************
 Table A.6: Robustness to alternative sample definitions for comparison group,
petty property crime
********************************************************************************
*******************************************************************************/


local titles2 "\multicolumn{1}{c}{\specialcell{Comparison group: \\ \;}}  & \multicolumn{1}{c}{\specialcell{Non-opioid \\ drug offenders}} & \multicolumn{1}{c}{\specialcell{Non-opioid \\ drug \& prop. \\ offenders }}  & \multicolumn{1}{c}{\specialcell{No \\ comparison \\ group}} & \multicolumn{1}{c}{\specialcell{No \\ comparison \\ group \\ w/ pre trend}} & \multicolumn{1}{c}{\specialcell{Comparison \\ group: \\ Non-opioid drug \\ property \\ and violent \\ offenders}}    \\"
local numbers " \cmidrule(lr){2-6} & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)}  & \multicolumn{1}{c}{(4)}  & \multicolumn{1}{c}{(5)}   \\ \hline "

esttab off_theft_nonmvA4_1 off_theft_nonmvA4_2 off_theft_nonmvA4_NC off_theft_nonmvA4_NC2 off_theft_nonmvA5_2      ///
using "results/TableA6.tex", replace f margin  ///
keep(pre post1 post2 post3) coeflabels(pre " Years -5 to -2" ///
post1 "Year 0" post2 "Year 1" ///
post3 "Years 2 to 7" )   ///
label booktabs b(4) p(4) alignment(S)  /// 
collabels(none)  ///
mlabels(none) nonumbers  ///
posthead( "`titles2'" "`numbers'" ) ///
star(*  0.05 ** 0.01) compress ///
width(\hsize) ///
cells("b(fmt(a2)star )" "se(fmt(a2)par)")  noobs nolines nonum ///
stats(  mean2000   , fmt(%9.2fc    ) ///
layout( "\multicolumn{1}{c}{@}"    ) ///
labels( "\hline \specialcell{Treatment group \\ avg. in Year -1  }"      ))


 /*******************************************************************************
********************************************************************************
Table A.7: Heterogeneity of effects by intensity of opioid use
********************************************************************************
*******************************************************************************/


use "data/panel.dta", clear

*shorten variable names for globals
gen off_drug_harduse=off_drug_hard_nonO_use
gen off_drug_cuse=off_drug_cocaine_use
gen off_drug_ause=off_drug_amph_use
gen off_violnonrobb=off_violent_nonrobb
gen off_proprobb=off_property_robb
gen off_sassault=off_assault_serious
gen off_cassault=off_assault_common
gen off_sprop=off_property_serious

global depvarsnofe "mortality mortality_opiates2 mortality_opiates mortality_nonopioid"
global depvarsfe1 " off_drug_harduse  off_drug_ause off_drug_cuse  off_violnonrobb off_proprobb   off_sassault off_cassault  off_sexassault off_harrassment off_murder_plus"
global depvarsfe2 "off_proprobb off_sprop off_robbery off_breakenter off_mv_theft off_theft_nonmv"

global indcontrols "treated i.female i.indig i.female_missing i.indig_missing  "
global agecontrols "i.age_int  i.age_int#i.female i.age_int#i.indig "
global offcontrols "i.first_drug_off_ym"

global coeftypes "post1 post3"


global indcontrols "treated i.female i.indig i.female_missing i.indig_missing  "
global agecontrols "i.age_int  i.age_int#i.female i.age_int#i.indig "
global offcontrols "i.first_drug_off_ym"

*no of opioid offenses pre shortage
preserve
use "data/offense_ym.dta", clear
keep if ym<=ym(2000,9)
collapse (sum) preshock_opiates_total=off_drug_opiates, by(ppn)
tempfile preop
save `preop'
restore
merge m:1 ppn  using `preop'
drop if _merge==2
drop _merge
gen het7=0
replace het7=1 if treated==1 & preshock_opiates_total==1
replace het7=1 if treated==0
gen het8=0
replace het8=1 if treated==1 & preshock_opiates_total>1
replace het8=1 if treated==0

*ever in phdas  pre shortage
preserve
use "data/phdas_ym.dta", clear
keep if ym<=ym(2000,9)
gen pre_inphdas=1
collapse (max) pre_inphdas maxdose=phdas_beg_ym_maxdose, by(ppn)
tempfile phdas
sum maxdose, detail
tempfile prephdas
save `prephdas'
restore
merge m:1 ppn using `prephdas'
drop if _merge==2
drop _merge
gen het10=0
replace het10=1 if treated==1 & pre_inphdas==1
replace het10=1 if treated==0

tab het10 if treated==1 //62% had treatement

gen het9=0
replace het9=1 if treated==1 & pre_inphdas==.
replace het9=1 if treated==0

*in phdas, maxdose 150 and below vs 150 above
gen het11=0
replace het11=1 if treated==1 & pre_inphdas==1 & maxdose<=150 & maxdose~=.
replace het11=1 if treated==0
gen het12=0
replace het12=1 if treated==1 & pre_inphdas==1 & maxdose>150 & maxdose~=.
replace het12=1 if treated==0

global depvarsnofe "mortality_opiates2 mortality"
global depvarsfe1 " off_drug_harduse  off_violnonrobb "
global depvarsfe2 "off_sprop "

forvalues j=7/12 {

	preserve 
	
	global At "Atyear_1995 Atyear_1996 Atyear_1997 Atyear_1998 Atyear_1999  Atyear_2001 Atyear_2002 Atyear_2003 Atyear_2004 Atyear_2005 Atyear_2006 Atyear_2007 Atyear_2008"
	
	foreach var of varlist $At {
	gen `var'_O=0
	replace `var'_O=`var' if het`j'==0 & treated==1
	replace `var'=`var' if het`j'==1 & treated==1
	replace `var'=0 if (het`j'==0 | treated==0)
}



if (`j'==11) {
	foreach var of varlist $At {
		gen `var'_O2=0 
		replace `var'_O2=`var'_O if het12==0 & treated==1
		replace `var'_O=0 if het12==0 & treated==1
	}

}
if (`j'==12) {
	foreach var of varlist $At {
		gen `var'_O2=0 
		replace `var'_O2=`var'_O if het11==0 & treated==1
		replace `var'_O=0 if het11==0 & treated==1
	}
}
	
*** Table - no ind. FE*
foreach var of varlist $depvarsnofe  {

capture drop R*
gen R`var'=`var'*1000*4

reg R`var' $indcontrols $agecontrols $offcontrols Atyear_*  Ayear_* q4y2000 q4y2000_T i.qtr treated,  vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
(pt: ((_b[Atyear_2001] + _b[Atyear_2002] + _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/8)) ///
(pre_O: (( _b[Atyear_1996_O]+ _b[Atyear_1997_O]+_b[Atyear_1998_O]+_b[Atyear_1999_O])/4)) ///
(post1_O: (_b[Atyear_2001_O])) /// 
(post2_O:  (_b[Atyear_2002_O])) ///
(post3_O: (( _b[Atyear_2003_O] + _b[Atyear_2004_O]+ _b[Atyear_2005_O] + _b[Atyear_2006_O] + _b[Atyear_2007_O] + _b[Atyear_2008_O])/6)) ///
(pt_O: ((_b[Atyear_2001_O] + _b[Atyear_2002_O] + _b[Atyear_2003_O] + _b[Atyear_2004_O]+ _b[Atyear_2005_O] + _b[Atyear_2006_O] + _b[Atyear_2007_O] + _b[Atyear_2008_O])/8)) ///
, post 


foreach c of global coeftypes {
global b_`var'_`c'_`j': di %6.2fc _b[`c']
global se_`var'_`c'_`j': di %6.2fc _se[`c']
test `c'=0
global `var'_`c'_`j'_p: di %12.2fc r(p)
*global `var'_`c'_`j'_star=cond(${`var'_`c'_`j'_p}<.01,"***",cond(${`var'_`c'_`j'_p}<.05,"**",cond(${`var'_`c'_`j'_p}<.10,"*","")))
global `var'_`c'_`j'_star=cond(${`var'_`c'_`j'_p}<.01,"**",cond(${`var'_`c'_`j'_p}<.05,"*",""))

test `c'=`c'_O
global `var'_`c'_`j'_pO: di %12.2fc r(p)
}
sum R`var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1 & e(sample) & het`j'==1
global ymean_`var'_`j': di %6.2fc r(mean)


}


*with individual FE
foreach var of varlist $depvarsfe1  {
capture drop R*
gen R`var'=`var'*1000*4

* Baseline - opioid outcomes

areg R`var' $indcontrols $agecontrols $offcontrols Atyear_*  Ayear_* q4y2000 q4y2000_T i.qtr  , absorb(ppn)  vce(cluster ppn)

local rsquare=e(r2)


nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
(pt: ((_b[Atyear_2001] + _b[Atyear_2002] + _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/8)) ///
(pre_O: (( _b[Atyear_1996_O]+ _b[Atyear_1997_O]+_b[Atyear_1998_O]+_b[Atyear_1999_O])/4)) ///
(post1_O: (_b[Atyear_2001_O])) /// 
(post2_O:  (_b[Atyear_2002_O])) ///
(post3_O: (( _b[Atyear_2003_O] + _b[Atyear_2004_O]+ _b[Atyear_2005_O] + _b[Atyear_2006_O] + _b[Atyear_2007_O] + _b[Atyear_2008_O])/6)) ///
(pt_O: ((_b[Atyear_2001_O] + _b[Atyear_2002_O] + _b[Atyear_2003_O] + _b[Atyear_2004_O]+ _b[Atyear_2005_O] + _b[Atyear_2006_O] + _b[Atyear_2007_O] + _b[Atyear_2008_O])/8)) ///
, post 


foreach c of global coeftypes {
global b_`var'_`c'_`j': di %6.2fc _b[`c']
global se_`var'_`c'_`j': di %6.2fc _se[`c']
test `c'=0
global `var'_`c'_`j'_p: di %12.2fc r(p)
*global `var'_`c'_`j'_star=cond(${`var'_`c'_`j'_p}<.01,"***",cond(${`var'_`c'_`j'_p}<.05,"**",cond(${`var'_`c'_`j'_p}<.10,"*","")))
global `var'_`c'_`j'_star=cond(${`var'_`c'_`j'_p}<.01,"**",cond(${`var'_`c'_`j'_p}<.05,"*",""))

test `c'=`c'_O
global `var'_`c'_`j'_pO: di %12.2fc r(p)
}

sum R`var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1 & e(sample) & het`j'==1
global ymean_`var'_`j': di %6.2fc r(mean)
}



*with individual FE - alternative control group for property 
foreach var of varlist $depvarsfe2  {
capture drop R*
gen R`var'=`var'*1000*4

* Baseline - opioid outcomes

areg R`var' $indcontrols $agecontrols $offcontrols Atyear_*  Ayear_* q4y2000 q4y2000_T i.qtr ///
if (treated==1 | (treated==0  & (pre_off_breakenter>0 | pre_off_mv_theft>0 | pre_off_robbery>0)))  ///
, absorb(ppn)  vce(cluster ppn)

local rsquare=e(r2)

nlcom (pre: (( _b[Atyear_1996]+ _b[Atyear_1997]+_b[Atyear_1998]+_b[Atyear_1999])/4)) ///
(post1: (_b[Atyear_2001])) /// 
(post2:  (_b[Atyear_2002])) ///
(post3: (( _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/6)) ///
(pt: ((_b[Atyear_2001] + _b[Atyear_2002] + _b[Atyear_2003] + _b[Atyear_2004]+ _b[Atyear_2005] + _b[Atyear_2006] + _b[Atyear_2007] + _b[Atyear_2008])/8)) ///
(pre_O: (( _b[Atyear_1996_O]+ _b[Atyear_1997_O]+_b[Atyear_1998_O]+_b[Atyear_1999_O])/4)) ///
(post1_O: (_b[Atyear_2001_O])) /// 
(post2_O:  (_b[Atyear_2002_O])) ///
(post3_O: (( _b[Atyear_2003_O] + _b[Atyear_2004_O]+ _b[Atyear_2005_O] + _b[Atyear_2006_O] + _b[Atyear_2007_O] + _b[Atyear_2008_O])/6)) ///
(pt_O: ((_b[Atyear_2001_O] + _b[Atyear_2002_O] + _b[Atyear_2003_O] + _b[Atyear_2004_O]+ _b[Atyear_2005_O] + _b[Atyear_2006_O] + _b[Atyear_2007_O] + _b[Atyear_2008_O])/8)) ///
, post 


foreach c of global coeftypes {
global b_`var'_`c'_`j': di %6.2fc _b[`c']
global se_`var'_`c'_`j': di %6.2fc _se[`c']
test `c'=0
global `var'_`c'_`j'_p: di %12.2fc r(p)
*global `var'_`c'_`j'_star=cond(${`var'_`c'_`j'_p}<.01,"***",cond(${`var'_`c'_`j'_p}<.05,"**",cond(${`var'_`c'_`j'_p}<.10,"*","")))
global `var'_`c'_`j'_star=cond(${`var'_`c'_`j'_p}<.01,"**",cond(${`var'_`c'_`j'_p}<.05,"*",""))

test `c'=`c'_O
global `var'_`c'_`j'_pO: di %12.2fc r(p)
}

sum R`var' if inrange(yq,yq(1999,4),yq(2000,3)) & treated==1 & e(sample) & het`j'==1
global ymean_`var'_`j': di %6.2fc r(mean)
}

restore
}





texdoc init "results/TableA7.tex", replace force
tex  & \multicolumn{2}{c}{\specialcell{No. of opioid \\ offenses at \\ supply shock }} & \multicolumn{2}{c}{\specialcell{Any methadone \\ treatment pre \\ supply shock?  }} & \multicolumn{2}{c}{\specialcell{Max dose of \\ treatment pre \\ supply shock  }} \\
tex \cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}   & \multicolumn{1}{c}{\specialcell{$=1$}} & \multicolumn{1}{c}{\specialcell{$>1$}} & \multicolumn{1}{c}{\specialcell{No}} & \multicolumn{1}{c}{\specialcell{Yes}} & \multicolumn{1}{c}{\specialcell{$\le150$ mg}} & \multicolumn{1}{c}{\specialcell{$>150$ mg}} \\ \hline

tex \\
tex &  \multicolumn{6}{c}{\specialcell{A. Opioid-related mortality}} \\
tex \\
tex Short term (yr 0) & \multicolumn{1}{c}{ ${b_mortality_opiates2_post1_7}${mortality_opiates2_post1_7_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post1_8}${mortality_opiates2_post1_8_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post1_9}${mortality_opiates2_post1_9_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post1_10}${mortality_opiates2_post1_10_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post1_11}${mortality_opiates2_post1_11_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post1_12}${mortality_opiates2_post1_12_star} }  \\
tex  &  \multicolumn{1}{c}{ (${se_mortality_opiates2_post1_7}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post1_8}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post1_9}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post1_10}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post1_11}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post1_12}) }  \\
tex Longer term (yrs 2-7)   &  \multicolumn{1}{c}{ ${b_mortality_opiates2_post3_7}${mortality_opiates2_post3_7_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post3_8}${mortality_opiates2_post3_8_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post3_9}${mortality_opiates2_post3_9_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post3_10}${mortality_opiates2_post3_10_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post3_11}${mortality_opiates2_post3_11_star} } & \multicolumn{1}{c}{ ${b_mortality_opiates2_post3_12}${mortality_opiates2_post3_12_star} }   \\
tex  &  \multicolumn{1}{c}{ (${se_mortality_opiates2_post3_7}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post3_8}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post3_9}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post3_10}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post3_11}) } & \multicolumn{1}{c}{ (${se_mortality_opiates2_post3_12}) } \vspace{0.25cm}  \\ 
tex   Avg in yr -1  & \multicolumn{1}{c}{${ymean_mortality_opiates2_7}} & \multicolumn{1}{c}{${ymean_mortality_opiates2_8}} & \multicolumn{1}{c}{${ymean_mortality_opiates2_9}} & \multicolumn{1}{c}{${ymean_mortality_opiates2_10}}  & \multicolumn{1}{c}{${ymean_mortality_opiates2_11}} & \multicolumn{1}{c}{${ymean_mortality_opiates2_12}}  \\

tex \\
tex &  \multicolumn{6}{c}{\specialcell{B. All-cause mortality}} \\
tex \\
tex Short term (yr 0)   &  \multicolumn{1}{c}{ ${b_mortality_post1_7}${mortality_post1_7_star} } & \multicolumn{1}{c}{ ${b_mortality_post1_8}${mortality_post1_8_star} } & \multicolumn{1}{c}{ ${b_mortality_post1_9}${mortality_post1_9_star} } & \multicolumn{1}{c}{ ${b_mortality_post1_10}${mortality_post1_10_star} } & \multicolumn{1}{c}{ ${b_mortality_post1_11}${mortality_post1_11_star} } & \multicolumn{1}{c}{ ${b_mortality_post1_12}${mortality_post1_12_star} }  \\
tex  &  \multicolumn{1}{c}{ (${se_mortality_post1_7}) } & \multicolumn{1}{c}{ (${se_mortality_post1_8}) } & \multicolumn{1}{c}{ (${se_mortality_post1_9}) } & \multicolumn{1}{c}{ (${se_mortality_post1_10}) } & \multicolumn{1}{c}{ (${se_mortality_post1_11}) } & \multicolumn{1}{c}{ (${se_mortality_post1_12}) }  \\
tex Longer term (yrs 2-7)  &  \multicolumn{1}{c}{ ${b_mortality_post3_7}${mortality_post3_7_star} } & \multicolumn{1}{c}{ ${b_mortality_post3_8}${mortality_post3_8_star} } & \multicolumn{1}{c}{ ${b_mortality_post3_9}${mortality_post3_9_star} } & \multicolumn{1}{c}{ ${b_mortality_post3_10}${mortality_post3_10_star} } & \multicolumn{1}{c}{ ${b_mortality_post3_11}${mortality_post3_11_star} } & \multicolumn{1}{c}{ ${b_mortality_post3_12}${mortality_post3_12_star} }   \\
tex  &  \multicolumn{1}{c}{ (${se_mortality_post3_7}) } & \multicolumn{1}{c}{ (${se_mortality_post3_8}) } & \multicolumn{1}{c}{ (${se_mortality_post3_9}) } & \multicolumn{1}{c}{ (${se_mortality_post3_10}) } & \multicolumn{1}{c}{ (${se_mortality_post3_11}) } & \multicolumn{1}{c}{ (${se_mortality_post3_12}) } \vspace{0.25cm}  \\ 
tex   Avg in yr -1   & \multicolumn{1}{c}{${ymean_mortality_7}} & \multicolumn{1}{c}{${ymean_mortality_8}} & \multicolumn{1}{c}{${ymean_mortality_9}} & \multicolumn{1}{c}{${ymean_mortality_10}}  & \multicolumn{1}{c}{${ymean_mortality_11}} & \multicolumn{1}{c}{${ymean_mortality_12}}  \\

tex \\
tex &  \multicolumn{6}{c}{\specialcell{C. Non-opioid drug use/possession}} \\
tex \\
tex Short term (yr 0)   &  \multicolumn{1}{c}{ ${b_off_drug_harduse_post1_7}${off_drug_harduse_post1_7_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post1_8}${off_drug_harduse_post1_8_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post1_9}${off_drug_harduse_post1_9_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post1_10}${off_drug_harduse_post1_10_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post1_11}${off_drug_harduse_post1_11_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post1_12}${off_drug_harduse_post1_12_star} }  \\
tex &    \multicolumn{1}{c}{ (${se_off_drug_harduse_post1_7}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post1_8}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post1_9}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post1_10}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post1_11}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post1_12}) }  \\
tex Longer term (yrs 2-7)   &  \multicolumn{1}{c}{ ${b_off_drug_harduse_post3_7}${off_drug_harduse_post3_7_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post3_8}${off_drug_harduse_post3_8_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post3_9}${off_drug_harduse_post3_9_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post3_10}${off_drug_harduse_post3_10_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post3_11}${off_drug_harduse_post3_11_star} } & \multicolumn{1}{c}{ ${b_off_drug_harduse_post3_12}${off_drug_harduse_post3_12_star} }   \\
tex  &  \multicolumn{1}{c}{ (${se_off_drug_harduse_post3_7}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post3_8}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post3_9}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post3_10}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post3_11}) } & \multicolumn{1}{c}{ (${se_off_drug_harduse_post3_12}) }  \vspace{0.25cm} \\ 
tex   Avg in yr -1  & \multicolumn{1}{c}{${ymean_off_drug_harduse_7}} & \multicolumn{1}{c}{${ymean_off_drug_harduse_8}} & \multicolumn{1}{c}{${ymean_off_drug_harduse_9}} & \multicolumn{1}{c}{${ymean_off_drug_harduse_10}}  & \multicolumn{1}{c}{${ymean_off_drug_harduse_11}} & \multicolumn{1}{c}{${ymean_off_drug_harduse_12}}   \\

tex \\
tex &  \multicolumn{6}{c}{\specialcell{D. Violent offenses (excl. robbery)}} \\
tex \\
tex Short term (yr 0)   &  \multicolumn{1}{c}{ ${b_off_violnonrobb_post1_7}${off_violnonrobb_post1_7_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post1_8}${off_violnonrobb_post1_8_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post1_9}${off_violnonrobb_post1_9_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post1_10}${off_violnonrobb_post1_10_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post1_11}${off_violnonrobb_post1_11_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post1_12}${off_violnonrobb_post1_12_star} }   \\
tex  &  \multicolumn{1}{c}{ (${se_off_violnonrobb_post1_7}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post1_8}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post1_9}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post1_10}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post1_11}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post1_12}) }  \\
tex Longer term (yrs 2-7)   &  \multicolumn{1}{c}{ ${b_off_violnonrobb_post3_7}${off_violnonrobb_post3_7_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post3_8}${off_violnonrobb_post3_8_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post3_9}${off_violnonrobb_post3_9_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post3_10}${off_violnonrobb_post3_10_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post3_11}${off_violnonrobb_post3_11_star} } & \multicolumn{1}{c}{ ${b_off_violnonrobb_post3_12}${off_violnonrobb_post3_12_star} }   \\
tex  &  \multicolumn{1}{c}{ (${se_off_violnonrobb_post3_7}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post3_8}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post3_9}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post3_10}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post3_11}) } & \multicolumn{1}{c}{ (${se_off_violnonrobb_post3_12}) }  \vspace{0.25cm} \\ 
tex   Avg in yr -1  & \multicolumn{1}{c}{${ymean_off_violnonrobb_7}} & \multicolumn{1}{c}{${ymean_off_violnonrobb_8}} & \multicolumn{1}{c}{${ymean_off_violnonrobb_9}} & \multicolumn{1}{c}{${ymean_off_violnonrobb_10}}  & \multicolumn{1}{c}{${ymean_off_violnonrobb_11}} & \multicolumn{1}{c}{${ymean_off_violnonrobb_12}}   \\

tex \\
tex &  \multicolumn{6}{c}{\specialcell{E. Serious property offenses (robbery, burglary, vehicle theft)}} \\
tex \\
tex Short term (yr 0)   &  \multicolumn{1}{c}{ ${b_off_sprop_post1_7}${off_sprop_post1_7_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post1_8}${off_sprop_post1_8_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post1_9}${off_sprop_post1_9_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post1_10}${off_sprop_post1_10_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post1_11}${off_sprop_post1_11_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post1_12}${off_sprop_post1_12_star} }   \\
tex  &  \multicolumn{1}{c}{ (${se_off_sprop_post1_7}) } & \multicolumn{1}{c}{ (${se_off_sprop_post1_8}) } & \multicolumn{1}{c}{ (${se_off_sprop_post1_9}) } & \multicolumn{1}{c}{ (${se_off_sprop_post1_10}) } & \multicolumn{1}{c}{ (${se_off_sprop_post1_11}) } & \multicolumn{1}{c}{ (${se_off_sprop_post1_12}) }  \\
tex Longer term (yrs 2-7)  &  \multicolumn{1}{c}{ ${b_off_sprop_post3_7}${off_sprop_post3_7_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post3_8}${off_sprop_post3_8_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post3_9}${off_sprop_post3_9_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post3_10}${off_sprop_post3_10_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post3_11}${off_sprop_post3_11_star} } & \multicolumn{1}{c}{ ${b_off_sprop_post3_12}${off_sprop_post3_12_star} }  \\
tex  &  \multicolumn{1}{c}{ (${se_off_sprop_post3_7}) } & \multicolumn{1}{c}{ (${se_off_sprop_post3_8}) } & \multicolumn{1}{c}{ (${se_off_sprop_post3_9}) } & \multicolumn{1}{c}{ (${se_off_sprop_post3_10}) } & \multicolumn{1}{c}{ (${se_off_sprop_post3_11}) } & \multicolumn{1}{c}{ (${se_off_sprop_post3_12}) }  \vspace{0.25cm} \\ 
tex   Avg in yr -1 & \multicolumn{1}{c}{${ymean_off_sprop_7}} & \multicolumn{1}{c}{${ymean_off_sprop_8}} & \multicolumn{1}{c}{${ymean_off_sprop_9}} & \multicolumn{1}{c}{${ymean_off_sprop_10}}  & \multicolumn{1}{c}{${ymean_off_sprop_11}} & \multicolumn{1}{c}{${ymean_off_sprop_12}}  \\

texdoc close

 
   
   
   
  
/*******************************************************************************
********************************************************************************

Figure B1:  

Trends in prescription opioid use, Australia and US

originally compiled from 
Source: Pain & Policy Studies Group, University of Wisconsin/WHO Collaborating Center, 2017, 
Citing International Narcotics Control Board and World Health Organization population data. 

This organization website no longer exits. Used this site
https://apps.automeris.io/wpd/
to trace out points and recreate graph 

Australia numbers include methadone, but US do not, so I take out methadone totals for Australia

********************************************************************************
*******************************************************************************/ 
 

 
 insheet using "data/prescriptionopioids_usa2.csv", clear
 
 rename v1 year
 rename v2 morphine_equivalence_US_total
 
 replace year=round(year)
 
 collapse (mean) morphine_equivalence_US_total, by(year)
  
 tempfile us2
 save `us2'
 
 
  insheet using "data/prescriptionopioids_aus_total.csv", clear
 
 rename v1 year
 rename v2 morphine_equivalence_total
 
 replace year=round(year)
 
 collapse (mean) morphine_equivalence_total, by(year)
  
 
  
  merge 1:1 year using `us2'
 drop _merge
 
 
 keep if inrange(year,1990,2015)
  
  twoway  (line morphine_equivalence_total year, lcolor(black) lwidth(thick) ) ///
 (line morphine_equivalence_US_total year, lcolor(black) lwidth(thick) lpattern(longdash)) ///
 , xlabel(1990(5)2015)  ylabel(0(200)800, angle(horizontal) format(%9.0fc)) ///
 scheme(s1mono) ytitle("Opioids prescribed" "(morphine equivalence in mg/per person)")  ///
 ysc(titlegap(0)) xsc(titlegap(2)) xtitle("Year")  ///
  legend(label(1 "Australia") label(2 "United States")  cols(1) ring(0) pos(5)) 

  graph export "results/FigureB1.pdf", replace
  
  
  
   
/*******************************************************************************
********************************************************************************

Figure B.2: Heroin prices in Australia and other countries, 1999
*******************************************************************************/

  
import delimited "data/international-prices.csv", clear

gen prices2=.
replace prices2=prices if country=="Australia"
gen prices1=prices
replace prices1=. if country=="Australia" 

graph hbar (asis) prices1 prices2, nofill over(country, sort(prices) descending) scheme(s1mono) ///
ytitle("Average retail price of heroin in 1999 (US$ per gram)") ///
bar(1, bfcolor(gs12)) bar(2, bfcolor(black)) legend(off)


 graph export "results/FigureB2.pdf", replace
 
 
 
 /***********************

Figure C1: Annual net value of mortality & crime effects 

********************************************************************************
*******************************************************************************/ 
  
insheet using "data/figure_npv.csv", clear


twoway line approach1 year, lcolor(black)  lw(thick) || ///
line approach2 year, lc(black) lp(dash) lw(thick) || ///
line approach3 year, lc(gray) lw(thick) ///
, yline(0) xlabel(2001(2)2008) ylabel(-50000(10000)10000, angle(horizontal)) ///
scheme(s1mono) xsc(titlegap(2)) xtitle("Year") ytitle("Net value per user (A$)") ///
legend(order(1 "With VSL estimates from Viscusi (2018) and" "victim costs from Australian Institute of Criminology" 2 "With higher VSL estimates from Viscusi and Masterman (2017)" 3 "With updated victim costs from Johnston et al. (2018)") cols(1) ring(0) position(1) size(small))

graph export "results/FigureC1.pdf", replace


